Working and testing redirects for Apache

by John H
5 minutes

Working and testing out redirects for Apache

To work on a testing local install of the site make sure you have a local installation of the site you are trying to put redirects in place for.

On that installation you will be working in the htaccess for it.

Using the htaccess file is good because it updates in real time. If you work from a Virtual host entry you will have to continuously restart the apache server.

In the htaccess file you will place your rewrite conditions , rewrite rules and redirect matches.

Remember to turn the RewriteEngine On, otherwise those conditions won't work. Also remember to keep your links to local urls relative - that way when you move servers it will still work

A straight up match looks like this


redirectMatch 301 ^/haiku-light-support https://www.bigassfans.com/support/haiku-light/

You can make some characters optional by adding ? - the dollar sign makes sure it only matches when the character is at the end

This match makes the trailing / optional

redirectMatch 301 ^/haiku-light-support/?$ https://www.bigassfans.com/support/haiku-light/

The organiztion of the command is

command command-type current-url resulting-url

Rewrites

RewriteRules and RewriteCond can be confusing. The best way I've found to handle this is using the RewriteRule to make blanket rewrites.

For instance I might one to make all of the cases that match goto one url,

RewriteEngine On    
RewriteCond %{REQUEST_URI} ^/match1/?$
RewriteCond %{REQUEST_URI} ^/match2/?$
RewriteRule ^(.*)$ https://www.bigassfans.com/resulting-url/ [R=301,NC,L]

This sends localhost/match1 and localhost/match2 to bigassfans.com/resulting-url/

RewriteCond %{REQUEST_URI} !^/match1/?$
RewriteCond %{REQUEST_URI} !^/match2/?$
RewriteRule ^(.*)$ https://www.bigassfans.com/resulting-url/ [R=301,NC,L]

This send every request that IS NOT match1 or match2 to resulting-url

What I've found the best to do is use the NOT operator to make any exceptions possible.

RewriteCond %{REQUEST_URI} !^/match1/?$
RewriteCond %{REQUEST_URI} !^/match2/?$
RewriteRule ^(.*)$ https://www.bigassfans.com/resulting-url-3/ [R=301,NC,L]

redirectMatch 301 ^/match1/?$ https://www.bigassfans.com/resulting-url-1/
redirectMatch 301 ^/match2/?$ https://www.bigassfans.com/resulting-url-2/

In this case all requests on the url goto resulting-url-3 EXCEPT match1 and match2. These slip through the RewriteRule and get matched on the redirectMatch instances below.

Apache will execute whichever rewrite it matches first SO if you have a condition that matches a url 2 ways you will have to use an exclusion in order for it to execute as you would expect.

For instance

WRONG WAY
RewriteRule ^(.*)$ https://www.bigassfans.com/resulting-url/ [R=301,NC,L]
RewriteRule ^/dir/(.*)$ https://www.bigassfans.com/resulting-url-2/ [R=301,NC,L]

in this instance bot
localhost/something
localhost/dir/somethingelse
will both be sent to /resulting-url

For these to work you would need to exclude requests for /dir/ from the first Rule

RIGHT WAY
RewriteCond %{REQUEST_URI} !^/dir/(.*)?$
RewriteRule ^(.*)$ https://www.bigassfans.com/resulting-url/ [R=301,NC,L]

RewriteRule ^/dir/(.*)$ https://www.bigassfans.com/resulting-url-2/ [R=301,NC,L]

now requests like localhost/dir/something can slip past the first Rule and match with the next match

Once you've got all of your redirects and rewrites working on your local you can move them into your conf file on production. If your relative links match then everything should work.

Using a wildcard token

You may need to send any request made to a directory to a matching directory at your resulting url. This can happen by passing a wildcard variable to a position in your rewrite. In following example any ending string will be appended in the resulting url.

RewriteRule ^/faq/(.*)$ https://www.bigassfans.com/support/faq/$1 [R=301,NC,L]

So localhost/faq/blah would be redirected to bigassfans.com/support/faq/blah
or localhost/faq/yada would be redirected to bigassfans.com/support/faq/yada

Pretty handy

Related Articles

Storing and sorting Values in PHP Array

I just had a huge ordeal dealing with an array.  I needed to loop through an array - process some...

John H John H
2 minutes

Blocking Access to a Url route in Apache

This serves a 403 forbidden for any route with blog. RewriteRule ^(.*/)?blog(.*/)?/ - [F,L]...

John H John H
~1 minute

Deleting records in AWS Cloud Search

Deleting All Records through SDK Managing documents is sucky in Cloud Search. The CloudSearch...

John H John H
~1 minute