Forum Moderators: phranque

Message Too Old, No Replies

Few questions on URL Rewriting

         

pro_seo

7:31 am on Mar 4, 2008 (gmt 0)

10+ Year Member



Friends,

I have a site which has a few dynamic URLs that I want to redirect into static ones. The site is in PHP

Now honestly I know a little less than nothing when it comes to URL Rewriting [in both Apache and IIS] and so I desperately seek your help here.

The URLs that I want to rewrite are like:

http://www.example.com/file_name.php?id=14

Now this is what I have done:

Using a URL rewriting tool, I have got a code which looks like:

RewriteRule file_name-id-(.*)\.html$ file_name.php?id=$1

I have placed this code within the htaccess and uploaded it.

Now when I try the page with the new URL I find that the page is opening with it which looks like:

http://www.example.com/file_name-id-14.php

But when I click on the link on the website navigation menu, the old page still shows up [the dynamic one]

A few questions:

While rewriting URLs, after placing the code, do we need to update the internal linking with the URL of the new pages?

OR

Is there a way by which I can click on the link to the old [dynamic] page and it will be automatically redirected [301 of course] to the new static URL. I am pretty sure there is a way of doing this which I don't know.

So please advise me on what should I do in a step-by-step order while rewriting URLs in APACHE.

Thanks a lot in advance!

[edited by: engine at 8:49 am (utc) on Mar. 6, 2008]
[edit reason] pleaase use example.com [/edit]

g1smd

12:29 pm on Mar 4, 2008 (gmt 0)

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



The URLs that your site respond to are defined by the URLs in the links on your pages.

In order to avoid duplicate content issues you need a redirect and a rewrite:

You redirect [R=301,L] the parameter-based URLs to the static-looking URLs.

You rewrite [L] the static-looking URLs to a parameter-based internal server filepath.

The difference between a redirect and a rewrite is crucial to the operation.

There are a lot of prior examples in this forum... the question gets asked and answered several times every week.

Beware of an inherent Duplicate Content issue in your implementation. I can link to www.example.com/anything-i-like-here-4141.html and the URL will be indexed as good. I am sure you would like to check that the preceding words are valid, and only allow one particular string to be indexed. In that case, your PHP script must check the requested URL and must either issue a 404 HTTP header (or a 301 to the correct URL) if the words are incorrect.

[edited by: engine at 8:49 am (utc) on Mar. 6, 2008]
[edit reason] please use example.com [/edit]

pro_seo

12:40 pm on Mar 4, 2008 (gmt 0)

10+ Year Member



Thanks for your response :)

However, can u give me the code by which I can 301 redirect the Dynamic URLs to their corresponding static ones.

I have searched for the code but just ain't getting it.

Thanks again!

jdMorgan

6:17 pm on Mar 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please see this earlier --and on-going-- discussion: [webmasterworld.com...]

Jim

pro_seo

7:09 am on Mar 5, 2008 (gmt 0)

10+ Year Member



Thanks Jim,

That link was very helpful. I also bookmarked another thread on URL_Rewrite tutorial.

Following these, I have done the rewrite myself and it does seem to work....except for:

when requesting for http://www.example.com/file_name.php?id=14 it gets redirected to
http://www.example.com/file_name-id-14.php?id=14

Now sure where that ?id=14 is coming from at the end of the URL [if I remove the ?id=14 the page still shows up]. Also I find that if I put in anything in the URL such as http://www.example.com/file_name-id-14jfdfkfdkjfk.php?id=14 the page still shows up but without the content.

Please advise me on how I should fix the both issues:

For your reference, below is the content of my htaccess

RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ http://www.example.com/$1 [R=301,L]

rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,L]

RewriteRule file_name-id-(.*)\.php$ file_name.php?id=$1 [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /file_name\.php\?id=([^&]+)\ HTTP/
RewriteRule ^file_name\.php$ http://www.example.com/file_name-id-%1.php [R=301,L]

Thanks a lot again!

[edited by: engine at 8:51 am (utc) on Mar. 6, 2008]
[edit reason] please use example.com [/edit]

g1smd

7:05 pm on Mar 5, 2008 (gmt 0)

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



Add a question mark to the end of the final redirected URL to suppress the parameters being added back one.

Beware also, that the rewrite directive must appear after all the redirects. You'll need to change the order of your rules.

pro_seo

6:23 am on Mar 6, 2008 (gmt 0)

10+ Year Member



Thanks! I added it and it worked...thanks a lot for it!

Regarding placing the rewrite after the redirect, you mean re-ordering the rules like:

RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ http://www.example.com/$1 [R=301,L]

rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /file_name\.php\?id=([^&]+)\ HTTP/
RewriteRule ^file_name\.php$ http://www.example.com/file_name-id-%1.php [R=301,L]

RewriteRule file_name-id-(.*)\.php$ file_name.php?id=$1 [L]

Please let me know if this order is okay?

Beware of an inherent Duplicate Content issue in your implementation. I can link to www.example.com/anything-i-like-here-4141.html and the URL will be indexed as good. I am sure you would like to check that the preceding words are valid, and only allow one particular string to be indexed. In that case, your PHP script must check the requested URL and must either issue a 404 HTTP header (or a 301 to the correct URL) if the words are incorrect.

Yes, I am seeing this as well. Can u advise me how I can set up a rule to 301 redirect all such invalid requests to the correct URL or issue a 404, depending on what's the best treatment for this as per your experience.

[edited by: engine at 8:52 am (utc) on Mar. 6, 2008]
[edit reason] Please use example.com [/edit]

g1smd

9:33 am on Mar 6, 2008 (gmt 0)

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



The reason you put the rewrite as the very last rule is that the (external) redirects interact with the browser causing the browser to make a new HTTP request each time, but the rewrite takes the final requested (canonical) URL and translates it into an internal server filepath. By then, we're done with external stuff. So, all of the redirect code needs to be before any rewrite code.

As for checking the validity of the words in the URL, your PHP script needs to do that. I assume it is a simple "are the words in the URL exactly the same as those in the title of this page". You can't do anything in .htaccess to check for this.

One more thing. You need to avoid a "redirection chain". So, your "catch-all" non-www to www code needs to be the final redirect before the rewrite. As you have it now, some non-www URLs would pass through two redirects. The most specific stuff needs to go first, and the more general rules last.

Consider what happens to a URL like example.com/file_name.php?id=12345 as you have it set up now. It passes through two redirects - a redirection chain - and that is often bad news for search engines.

pro_seo

11:48 am on Mar 6, 2008 (gmt 0)

10+ Year Member



Thanks a ton man - that was super useful!

So as per your recommendations I would place the codes like:

RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ http://www.example.com/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /file_name\.php\?id=([^&]+)\ HTTP/
RewriteRule ^file_name\.php$ http://www.example.com/file_name-id-%1.php? [R=301,L]

rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,L]

RewriteRule file_name-id-(.*)\.php$ file_name.php?id=$1 [L]

I guess this is okay now.