Forum Moderators: phranque

Message Too Old, No Replies

OMG mod_rewite again

regexp does my head in...

         

chubba

12:28 pm on Aug 2, 2004 (gmt 0)

10+ Year Member



Hi lovely people,

I like mod_rewrite but it hurts my brain...

I have searched this forum and many other sources and have even had a stab at creating my own. Yes I have also rtfm.

Please help me with a rule for sending requests for .shtml files to the same name but .html extension.

It does not sound like a hard job but has had me stumped all weekend.

Thanks in advance

Chubba

Birdman

1:36 pm on Aug 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteRule ^(.*)\.shtml $1.html [L]

That should do it. If you want it to send a redirect header, use the R flag as well.

[L,R=301]

chubba

10:02 pm on Aug 31, 2004 (gmt 0)

10+ Year Member



Hi Again,

I have tried the above and no joy :o(

I have checked htaccess files being processed OK so cannot understand why this is not working...

My htaccess file looks like:

=======================

RewriteEngine On
RewriteCond %{HTTP_HOST}!^www\.mysite\.co.uk
RewriteRule (.*) [mysite.co.uk...] [R=301,L]

RedirectMatch permanent /mypage.shtml /mypage/index.html

RewriteEngine On
RewriteRule ^(.*)\.shtml $1.html [L]

=================================

I have the RedirectMatch in there as well to fix another little problem I had, this and the first Rewrite rule work OK (redirecting [mysite.co.uk...] to [mysite.co.uk)...] so guess there is something not quite right with the rule to redirect .shtml to the same file name but with a .html extension?

Chubba

jdMorgan

10:17 pm on Aug 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code you have to do the .shtml to .html *rewrite* is not a redirect. It will indeed server the contents of an .html file whenever the corresponding .shtml file is requested, but it will do so entirely within the server, no redirect is involved. Therefore you will not see the address change in your browser address bat, and search engine spiders will not update the URL they use to fetch that page.

I would suggest a couple of minor adjustments, but what you have should work.


RewriteRule ^([^.]+)\.shtml$ /$1.html [L]

Can you be more specific about whether that works and whether you actually want an external redirect, rahter than an internal rewrite?

Jim

chubba

10:58 pm on Aug 31, 2004 (gmt 0)

10+ Year Member



Hiya jdMorgan,

Thank you for replying.

I guess that has taught me more about rewrite and redirect - I had never considered a difference between a rewrite and a redirect, I had considered them the same.

/blushes

My apol's for not being more specific, just a n00b doing his n00b thang :~)

I have tried the code you kindly gave me and alas this has not worked either.

OK - the scenario is that I have a site that contains all .shtml files, for my own selfish reasons I would like them all to be .html files. I have remade the files as .html files and when users (and robots) hit a .shtml file I would like them to be *redirected* to a .html file that has the same name so that a request for mypage.shtml would be permenantly (401 header) redirected to mypage.html and likewise a request for /mydirectory/myfile.shtml would be 401'd to /mydirectory/myfile.html.

Although from what you have said my request for myfile.shtml could actually be serving myfile.html but as it is not a redirect then the address does not change in the browser? That is wild and certainly opens up another world within mod_rewrite that I had not considered...

Thanks for clearing up my confusion with redirect and rewrite - I do feel really stupid now :o( Maybe I should rtfm a bit closer.

Many Thanks

Chubba

jdMorgan

11:37 pm on Aug 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Although from what you have said my request for myfile.shtml could actually be serving myfile.html but as it is not a redirect then the address does not change in the browser? That is wild and certainly opens up another world within mod_rewrite that I had not considered...

Yes, test it for yourself and see.

"That has not worked" is not really very useful as a status report, by the way. Please say how you tested, and what results you got. It will minimize the number of questions/answers, take less time for all involved, and result in a quicker fix for you.

To change your rule from an internal rewrite into a redirect, use:


RewriteRule ^([^.]+)\.shtml$ http://www.mysite.co.uk/$1.html [R=301,L]

Jim

chubba

9:31 am on Sep 1, 2004 (gmt 0)

10+ Year Member



Thanks!

That worked ;O)

OK - points taken and I will be more specific next time. Yup, understand that I could have checked the rewrite was working by changing the HTML file and requesting the SHTML file and seeing if the server served the amended page.

Thanks again as this has taught me a huge amount - apol's for not making this easier for you & me.

Chubba