Forum Moderators: phranque

Message Too Old, No Replies

htm to php and folder redirect

htaccess redirect

         

waytech

5:16 pm on Jan 26, 2015 (gmt 0)

10+ Year Member



I have been given a website to host and the prior setup was a mix of htm, html, & php pages. These all were also in a subfolder I will call SUB1. There are a lot of pages and they are indexed in Google & Bing so I need to redirect to my new settings.

First off all the pages are now php pages, so all htm & html need to be sent as php.
In my testing the following seems to do that.

RewriteRule ^(.+)\.html?$ /$1.php [R=301,NC]

Now I need to remove the "/SUB1/" folder from every url that is bookmarked that way. Examples are:

http://example.com/SUB1/index.htm
needs to be...
http://example.com/index.php

and

http://example.com/SUB1/Sub2/index.htm
needs to be...
http://example.com/Sub2/index.php

As you can see I only want the first "/SUB1/" stripped when someone tries to get here from a search engine or a browser bookmark.

How do I add a second rule to do this?
I have tried adding the code below, but that fails.

RewriteRule ^SUB1/(.+)$ http://example.com/$1 [R=301,NC]

penders

6:43 pm on Jan 26, 2015 (gmt 0)

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



Define "fails"?

Your rules actually look OK, except... can you combine ".html?$" with the second rule in order to avoid two redirects on occasions? You probably need the L flag on both directives and maybe you don't really need the NC flag?

Maybe rule#2 should come first?

topr8

7:30 pm on Jan 26, 2015 (gmt 0)

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



just a quick question ... regarding the parsing as php issue, why don't you just set the server to process .htm and .html as php? would save a rewrite?

waytech

8:44 pm on Jan 26, 2015 (gmt 0)

10+ Year Member



I found that this works...
RewriteRule ^(.+)\.html?$ /$1.php [R=301,NC]
RewriteRule ^SUB1/(.+)$ /$1 [R=301,NC]


This did not...
RewriteRule ^(.+)\.html?$ /$1.php [R=301,NC]
RewriteRule ^SUB1/(.+)$ http://example.com/$1 [R=301,NC]

Thanks for the replies

penders

9:50 pm on Jan 26, 2015 (gmt 0)

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



This did not...


? Apart from the second one using an absolute URL in the substitution, they look the same to me!? (Mmmm, I'd put that down to "chance" - browser caching perhaps?!)

lucy24

11:01 pm on Jan 26, 2015 (gmt 0)

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



I found that this works...
RewriteRule ^(.+)\.html?$ /$1.php [R=301,NC]

RewriteRule ^SUB1/(.+)$ /$1 [R=301,NC]

This did not...
RewriteRule ^(.+)\.html?$ /$1.php [R=301,NC]

RewriteRule ^SUB1/(.+)$ http://example.com/$1 [R=301,NC]

Since the patterns are identical, there's something you're not telling us. Note that I've added a space to stress that each version is two rules, and only the second rule of each pair is different. There are times when a RewriteRule will interpret the pattern differently depending on whether there's a protocol-plus-domain in the target. But that shouldn't apply when you've got an explicit [R] flag.

You don't need the [NC] flag unless your old site genuinely used all possible permutations of html, HTML, Html, hTmL and so on. For the human writer, [NC] is just a couple of letters. But for the server, it converts the whole thing into
\.[Hh][Tt][Mm][Ll]


regarding the parsing as php issue, why don't you just set the server to process .htm and .html as php? would save a rewrite?

There's that, too. But this is only practical if all files in a given directory are to be parsed as php. Otherwise the server is doing a lot of needless work. Kinda the same thing as parsing .html for SSIs in this respect.

Some people at this point would put in a strong argument for going extensionless.

Now then... the pattern
(.+)\.html

is not as efficient as it could be. (I think g1smd had a fixed phrase involving "greedy and promiscuous", like that one nephew everyone has.) There are alternatives; the easiest is
^([^.]+)\.htm

if your file and directory names never contain literal periods.

Unless you've got some very weird naming, you don't need a closing anchor, and therefore don't need the l? at the end: just let the server match as far as "htm" and it can get out of there. Since you're issuing an external redirect, it wouldn't even matter if there were extraneous garbage after the "htm" or "html".

http://example.com/SUB1/index.htm
needs to be...
http://example.com/index.php
and
http://example.com/SUB1/Sub2/index.htm
needs to be...
http://example.com/Sub2/index.php

Uhm, no, they don't. They need to be
http://example.com/

and
http://example.com/sub2/

without explicit "index.anything", where both are expressed as the single conditionless rule

RewriteRule ^sub1/(\w+/)?index\.htm http://example.com/$1 [R=301,L]


Normally an index redirect is your second-to-last redirect; here it should come before the generic rule

RewriteRule ^sub1/(.*) http://example.com/$1 [R=301,L]

Dideved

7:46 pm on Jan 29, 2015 (gmt 0)

10+ Year Member



> Now then... the pattern (.+)\.html is not as efficient as it could be. There are alternatives; the easiest is ^([^.]+)\.htm

We can -- and have -- put that claim to the test, and it's flatly false. It *isn't* more efficient. The only difference is your alternative mishandles a swath of valid URLs.

</dose-of-reality>