Forum Moderators: phranque

Message Too Old, No Replies

Having .htaccess mod rewrite problem with my new client website

Having .htaccess mod_rewrite problem with my new client website

         

smuralii

5:10 am on May 7, 2010 (gmt 0)

10+ Year Member



We have got a new SEO project which was developed by an another company with custom PHP content management system. They have tried to implement SEO friendly URL structure in that website. When we receive the website and checked, we found that the URL rewrite option using .htaccess is not correct and which ends up with 302 redirection type. Below is the code present in .htaccess,

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*)$ http://www.example.com/$1 [R=301,L]

RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)\.html$ seo.php?menuname=$1

RewriteCond %{THE_REQUEST} ^.*/home.php
RewriteRule ^(.*)home.php$ http://www.example.com/$1 [R=301,L]


I am pretty confident that the first one and the 3 one are implemented correct.
So please check this .htaccess code and help me to rewrite the code for 301 redirection type.
Also let me know if any other information needed...

[edited by: jdMorgan at 1:38 pm (utc) on May 7, 2010]
[edit reason] Please use example.com only. See Terms of Service and forum Charter. [/edit]

jdMorgan

2:12 pm on May 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Fairly bad code -- inefficient code snippets, and incorrect rule order as well... Try:

RewriteEngine on
#
# Externally redirect direct client requests for "home.php" in any directory to "/" in that same directory
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*home\.php([?#][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)home\.php$ http://www.example.com/$1 [R=301,L]
#
# Externally redirect requests for non-blank non-canonical hostnames to canonical hostname
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#
# Internally rewrite all requests for .html URL-paths to "seo.php" script
RewriteRule ^([a-z0-9_-]+)\.html$ seo.php?menuname=$1 [NC,L]

When constructing your code, put access controls first, followed by all external redirects in order from most-specific patterns and conditions (fewest URLs affected) to least-specific (more URLs affected), and finally internal rewrites, again in order from most- to least-specific.

Jim

g1smd

6:10 pm on May 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm not sure if the [NC] flag is a good idea on the final rewrite. It can promote Duplicate Content problems if the script doesn't check the casing is exactly right and then send a 404 error if it is incorrect.

There's a final step that can also be added. To make sure that no-one can directly access URLs like
example.com/seo.php?menuname=<something>
and therefore prevent Duplicate Content issues, you can prefix the whole lot with:

# Prevent direct access via dynamic URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*seo\.php\?menuname=([^\ ]+)\ HTTP/
RewriteRule ^([^/]+/)*seo\.php$ http://www.example.com/%1.html [R=301,L]

smuralii

5:33 am on May 8, 2010 (gmt 0)

10+ Year Member



Thanks for correcting me morgan.

Thanks for your reply. I have used both the codes in my but i still get the same problem what i have got earlier.

For the 1st code, the redirection works fine but the type of redirection is 302.
For the 2nd code, the redirection happens but i am getting file not found (404) error.

So please advice me on this..

g1smd

5:47 am on May 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For the code I supplied, look at the URL it redirects to, you can see it is wrong.

There's a typo in the code. The %1 should be %2 here.

smuralii

5:54 am on May 8, 2010 (gmt 0)

10+ Year Member



Still i am getting 404 (File not found error)..

g1smd

5:56 am on May 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Make sure you flush the browser cache before each test.

Use the Live HTTP Headers extension for Firefox to look at the server transaction. Note the requested URLs and the returned status codes.

smuralii

9:47 am on May 8, 2010 (gmt 0)

10+ Year Member



I have checked again my website as you said, but still having the same problem (404)...

g1smd

6:27 pm on May 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What URL did you request? Use example.com not a real domain name in the answer.

What URL did you see in the browser URL bar?

What does the HTTP Live Headers extension for Firefox report for this access?

Note the requested URLs and the returned status codes.

smuralii

3:42 pm on May 10, 2010 (gmt 0)

10+ Year Member



This is my actual link i check (http://www.example.com/navigation.php?id=2) and the resulting page is (http://www.example.net/about-us.html). The person who developed this site have done some coding with PHP and re-written all the pages also he used some htaccess code to do some redirection and url re-write. But with the present code i am getting the result page correctly, but the problem is the redirection status i receive is 302 type.

The URL i see in the browser bar before using your htaccess code for this URL (http://www.example.com/navigation.php?id=2) is http://www.example.net/about-us.html but with 302 redirection type.

The URL i see in the browser bar after using your htaccess code for this URL (http://www.example.com/navigation.php?id=2) is http://www.example.net/about-us.html but with initially 302 and 301 (for canonical url removal) and finally 404 header type (using LiveHTTP Headers).

I think you will get a clear idea of this. Please let me know if any other details needed.

jdMorgan

6:15 pm on May 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The 302 redirect is not coming from the code posted in this thread.

You need to fix that issue first, because it affects the code posted here.

Jim

smuralii

8:47 pm on May 10, 2010 (gmt 0)

10+ Year Member



Yes Morgan, i too know that the code does not give 302 type but the way they have implemented this is bit complicated. As i am SEO person, i am not good in coding, So i have informed the same to the developers, they too tried and could not fix this issue. So can you please explain in brief how to solve this issue.

jdMorgan

1:45 am on May 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, the best thing you could do would be to hire a local contractor to take your code apart and fix it. Check on-line and with the local universities -- This is a bigger problem than can be handled in a forum.

Jim

smuralii

3:17 pm on May 11, 2010 (gmt 0)

10+ Year Member



Thanks for your suggestion. I will ask the owner of the website to get back to the developer who developed this and ask them to fix this issue.

jdMorgan

4:21 pm on May 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A possible/likely cause of this is a very common error: Redirecting the client when all that is needed is an internal rewrite. Or in scripting terms, generating a 302 redirect when all that is needed is to "include" or "require" the content into the script and output it.

A redirect is a server response which tells the client "The content you asked for has moved. Ask for it again at this new URL." This terminates the current HTTP transaction, and requests that the client start a new one.

A rewrite simply translates the requested URL to a filepath which is different from the "default" URL-to-filepath translation.

So depending on where this redirect is taking place, it's either a 302 redirect when an internal rewrite would be better (mod_rewrite), or a 302 redirect, when all that is needed is to read the required content from disk (or from a database) and output it, or to directly invoke a second script that will generate and output the requested content. Since I do not have intimate knowledge of how your site functions, I can't tell you which method is being used or the exact cause, but something somewhere is generating a 302 redirect when it should not be doing that.

Because a redirect (closing the current HTTP transaction and opening a new one) takes time, using a redirect when a rewrite, include, or script call would work is wasteful of your visitor's time, and wasteful of both internet bandwidth and server resources.

Jim