Mobile app version of vmapp.org
Login or Join
Gonzalez347

: Using a .htaccess to RewriteRule and Redirect 301 at the same time? I have a couple of specific URLs that I want to display differently on my website, like this: RewriteRule ^contact$ contact.php

@Gonzalez347

Posted in: #Apache #Htaccess #Redirects #Url #UrlRewriting

I have a couple of specific URLs that I want to display differently on my website, like this:

RewriteRule ^contact$ contact.php


To get ride of the ".php" extension.

And to be SEO friendly (to avoid having 2 different URLS pointing to the same page), I also want to do a 301 redirect between the old URL and the new one:

Redirect 301 /contact.php www.example.com/contact


Each of the lines above works well separately. But if I add them both in my htaccess, I have a redirect loop, and nothing is displayed on my website. How can I fix that?

In the end, if I either type "/contact" or "/contact.php", I want to see the contact page with the url "/contact".

10.04% popularity Vote Up Vote Down


Login to follow query

More posts by @Gonzalez347

4 Comments

Sorted by latest first Latest Oldest Best

 

@Heady270

I had to ask this question myself. It is surprisingly tricky to do. Olaf Dietsche provided the solution of using an environment variable:

RewriteRule ^contact$ contact.php [L,E=CLEAN_CONTACT_URL:1]
RewriteCond %{ENV:REDIRECT_CLEAN_CONTACT_URL} !1
RewriteRule ^contact.php$ /contact [R=301,L]


That way the redirect only gets executed when contact.php is the actual URL, not the rewritten URL.

I tested it with my own server to verify that it works:

$ curl -s --head example.com/contact.php HTTP/1.1 301 Moved Permanently
Date: Sun, 27 Jul 2014 16:28:01 GMT
Server: Apache
Location: example.com/contact Content-Type: text/html; charset=iso-8859-1

$ curl -s --head example.com/contact HTTP/1.1 200 OK
Date: Sun, 27 Jul 2014 16:28:01 GMT
Server: Apache
Content-Type: text/html; charset=UTF-8


The reason that you set the CLEAN_CONTACT_URL environment variable but then read it as REDIRECT_CLEAN_CONTACT_URL is explained here.

10% popularity Vote Up Vote Down


 

@Debbie626

You are mixing the mod_alias and mod_rewrite modules, which means that the end result is often unexpected.

You should only use mod_rewrite in your case, that is, repöace your

Redirect 301 /contact.php www.example.com/contact

with

RewriteRule contact.php www.example.com/contact [R=301,L]


And then, as Dave mentioned, you should use the [L] flag to stop RewriteRule processing.

10% popularity Vote Up Vote Down


 

@Ogunnowo487

Use the [L] flag on your rewrite rule and place your redirect before it. (I'm going to use a rewrite here as well)

RewriteRule ^/contact.php$ www.example.com/contact [R=301,L]
RewriteRule ^contact$ /contact.php [L]


When the first rule is encountered it matches the /contact.php request and is the last rule processed. The redirect to /contact does not match the first rule but it will match the second which is served up transparently from /contact.php

I haven't tested this but it should work.

10% popularity Vote Up Vote Down


 

@Jamie184

Yes you would. You are telling Apache to take any reference to contact and rewrite the URI to contact.php then telling Apache to take any contact.php reference and change it to /contact which gets captured again by the first rule.

Here is what you are missing. For each rewrite or redirect, Apache makes the change then starts the whole matching process over again. So after your RewriteRule, Apache is converting the request to contact.php and after your Redirect, Apache is converting the request almost back to what it was originally.

If you are using the file name contact.php for SEO purposes, I would drop this concept. File names do help as much as the (apparent) directory name. As well, single keyword file names do not seem to matter much at all especially when it is such a common word. There are exceptions of course. Brand names with product names seem to matter. In this case, just do things the old fashioned way- a proper link and no fancy stuff.

Your redirect is correct. I would use it, however, I would not use specifically named PHP files such as contact.php. It just confuses things. Instead, I would stick with index.php which should be your DirectoryIndex. Unless you have several files within your /contact directory, this should work okay. If you do this, you can remove your RewriteRule and everything should work fine.

Clear as mud?

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme