All the Joomla htaccess redirects you will ever need

htaccess_code.png

Here are the quick code snippets for common tasks you need to do in .htaccess to do redirects for Joomla, or any site for that matter.  Most are linked to its own blog post that explains it in more detail.

Htaccess redirectsIn all reality, I am sure this post is missing several that would be helpful.  If it isn’t listed, please comment on post and let me know which others should be on the list.


HTTP Redirects

Force HTTPS on whole site

RewriteCond %{HTTPS} off

RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Force HTTP on whole site

RewriteCond %{HTTPS} on

RewriteCond %{REQUEST_URI} !protected [NC]

RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]


WWW Redirects

Force non-www to www – when forcing any protocol to https

RewriteCond %{HTTP_HOST} !^www\.

RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Force non-www to www – http explicit (multi protocol)

RewriteCond %{HTTP_HOST} !^www\.

RewriteCond %{HTTPS} off

RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Force non-www to www – https explicit (multi protocol)

RewriteCond %{HTTP_HOST} !^www\.

RewriteCond %{HTTPS} on

RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


Force www to non-www – when forcing any protocol to https

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]

RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

Force www to non-www – http explicit (multi protocol)

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]

RewriteCond %{HTTPS} off

RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Force www to non-www – https explicit (multi protocol)

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]

RewriteCond %{HTTPS} on

RewriteRule ^(.*)$ https://%1/$1 [R=301,L]


Joomla 3.8 Experimental URL Routing

Redirect old URLS with id to new ones

RewriteCond %{REQUEST_URI} (.*)\/(\d{1,}-|)(.*)\/(\d{1,}-|)(.*)
RewriteRule ^(.*)(\/)(.*) %1/%3/%5 [L,QSA,R=301]

Click on Joomla 3.8 Experimental URL Routing title link for more info.


Trailing Slash redirects

Remove trailing slash

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} (.+)/$

RewriteRule ^ %1 [R=301,L]

Add trailing slash

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} /(.*)/$

RewriteRule ^ /%1 [R=301,L]


Remove index.php from Joomla SEF Links

Remove from middle of url

RewriteCond %{REQUEST_URI} ^/index\.php/

RewriteRule ^index.php/(.*) /$1 [R=301]

Remove from end of url

RewriteCond %{REQUEST_URI} !^.*/administrator/index\.php [NC]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?) [NC]

RewriteRule ^ /%1 [R=301,L]


Removing the Temporary Url ~username out of Your Link

1. Comment Out with a #:

RewriteBase /~username/

2. Then add redirect, subbing out with your username:

RewriteEngine On

RedirectMatch 301 ^/~username(.*)$ $1


Redirect IP address to domain

RewriteCond %{HTTP_HOST} ^111\.111\.11\.111$

RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301]


Change URL from old folder to new folder

RewriteEngine On

RewriteRule ^oldfolder/(.*)$ /newfolder/$1 [R=301,NC]


Redirect pages after removing/disabling language code

RewriteEngine On

RewriteRule ^en/(.*)$ /$1 [R=301,NC]

or

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/en(.*)$

RewriteRule ^en/(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

if you also need to remove a .html then

RewriteCond %{REQUEST_URI} ^/en(.*)$

RewriteRule ^en/(.*)\.html$ https://%{HTTP_HOST}/$1 [R=301,L]

if you need to remove a index.php and .html then

RewriteCond %{REQUEST_URI} ^/index\.php/en(.*)$

RewriteRule ^index.php/en/(.*)\.html$ https://%{HTTP_HOST}/$1 [R=301,L]


Redirect Joomla 1.0 SEF links for Joomla 3.x

RewriteBase /

RewriteRule ^content/view/(.*)$ /index.php?option=com_content&view=article&id=$1 [L,R=301] 

That will redirect http://www.mysite.com/content/view/1234/2/ to  http://www.mysite.com/index.php?option=com_content&view=article&id=1234/2


Move website, remove folder name from path

RewriteCond %{THE_REQUEST} ^GET\ /folder/(.*)

RewriteCond %{THE_REQUEST} ^GET\ /folder/(.*)

RewriteRule ^folder/(.*) http://www.example.com/index.php/$1 [L,R=301]

or shorter version:

RewriteRule ^folder/(.*)$ /$1 [L,R=301]


Remove .html from URL

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)\.html$ /$1 [L,R=301]


Force domain to HTTPS but set subdomain to HTTP


Here are some others I plan to add later when I get time:

  • Remove Query String from link
  • Add folder to link
  • Match folder and remove everything before it
  • Redirect one folder to another folder
  • Redirect subdomain to folder
  • Match query string and redirect to page

Common Problems and Things to Check

If any of these aren’t working, please double check that you have the RewriteEngine On and have substituted the appropriate placeholders (mysite.com, folder, username, etc)

If you are getting 500 server errors, try enabling/disabling the various commands that start with “Options,” like Options +Indexes and Options +FollowSymLinks. Their usage varies widely between hosting companies where having it or not having it will throw 500 server errors.

If you are getting double index.phpindex.php then find these lines:


########## Begin – Rewrite rules to block out some common exploits
RewriteCond %{QUERY_STRING} proc/self/environ [OR]


and add in this extra conditional:


########## Begin – Rewrite rules to block out some common exploits
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{QUERY_STRING} proc/self/environ [OR]

Bonus Items

Also, if you need to tinker with the code, this tool by madewithlove is the best thing since sliced bread: .htaccess Tester

A great starting point for comprehensive security tweaks and so forth is the Master Htaccess written by the creator of Akeeba Backup and Admin Tools: Download Master .htaccess

About YellowWebMonkey

YellowWebMonkey Web design offers reliable website design, SEO and digital marketing services for Joomla, WordPress and Shopify sites. We strive to be a one-stop shop for all your web needs.

Recent Posts

Follow Us