Forum Moderators: phranque

Message Too Old, No Replies

.htaccess for only one directory

for changing to PHP extensions

         

old_expat

2:43 am on Jun 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm changing all the pages in one directory to PHP

This is what I have come up with. Is this the correct approach?

RedirectMatch 301 (.*)\.htm$ [mysite.com...]

jdMorgan

1:30 am on Jun 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That looks like it might give you a double-slash problem, and the regex is 'non-optimal'. I'd suggest:

RedirectMatch 301 [b]^/([^.]+)[/b]\.htm$ http://www.mysite.com/this-directory/$1.php

There's still one potential problem or point to consider: A 301 redirect 'exposes' your new URLs, and there may not be any good reason to do that. You could simply 'map' the new php pages to the old URLs, and no-one but you would even be aware that anything had changed. There's really no reason to expose the technology of the site, since html/php/whatever is meaningless to search engines and to most non-techie people. If you do a 301, then you risk a temporary disruption of your pages' rankings. If there's a good reason to do that, then go ahead, but it's usually not even needed.

So, you could use mod_rewrite, and just point the old html URLs to php like this:


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

the result of which would be that the old URLs could still be used, and would simply call up the php files of the same name, with no-one the wiser.

(I assume the code is located in the subdirectory itself; If not, then add that path to the beginning of the pattern.)

Jim

old_expat

7:10 am on Jun 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Jim,

So, you could use mod_rewrite, and just point the old html URLs to php like this:

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

the result of which would be that the old URLs could still be used, and would simply call up the php files of the same name, with no-one the wiser.

Yes, this is actually what I wanted to do. Sometimes my thinking get a little .. um .. "off track".:)

(I assume the code is located in the subdirectory itself; If not, then add that path to the beginning of the pattern.)

Yes, I'm going through my largest site one directory at a time and in this one I needed to add some dynamic features to each page.

Thanks for your continuing help, Jim.

dave

old_expat

8:23 am on Jun 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm .. didn't work ..

maybe I made a mistake. Here is what I did.

I made up an .htaccess file and uploaded to [mysite.com...]

That directory is where all the new *.php files are located

This is what I put in the .htaccess

[quote]RewriteEngine on
RewriteRule ^/([^.]+)\.html$ /this directory/$1.php [L][quote]

I tried it with and without the "RewriteEngine on" line

Tried ith with .htm$ and .html$

Tried it in the public_html .htaccess as well

I have a file called "thisfile.php" so I just changed the address in the browser from:

[mysite.com...]
to
[mysite.com...]

I get a 404

Tried several other files in that directory .. all 404

jdMorgan

2:10 pm on Jun 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry -- my cut-n-paste error...

The leading slash on the local URL-path is always stripped in .htacccess:


RewriteEngine on
RewriteRule [b]^([[/b]^.]+)\.html$ /this directory/$1.php [L]

might work better.

Jim

old_expat

4:04 pm on Jun 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




might work better.

Yep .. thanks.:)