Rewrite rule


When checking one of my domain names on Google I found some strange links and my host say it is due to this rewrite rule:

RewriteRule (.*)\.htm$ display.php?photo=$1

On my old host they had a different file structure for my sites and there was no problem - from memory all the sites were in different directories and each had a .htaccess file. This host has one site in public_html containing one site that needs the redirect and the other two sites in directories within this site.

From what I have found out (.*) means redirect all but I can not find how to only redirect one domain name. I have tried changing it to:

RewriteRule ^\.htm$ display.php?photo=$1

But all I get is a 404 error but the URL is looking correct

Please can you tell me where I am going wrong.


I don't really have my htaccess head on right now, but thinking out loud, maybe a condition before the rule something like:-

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*)\.htm$ display.php?photo=$1


That seems to do the job thank you @SamA74

Out of interest should I add a [L] after the rule?

After this I have some more rewrite rules which I do not know if they still work as they are some the hosts have inserted; do the RewriteCond overwrite each other? Should I move the two lines to the end of the .htaccess file to be sure?


I think so, that should stop the condition affecting subsequent rules.


I was unsure as from memory I read something like [L] meant "stop the process". I was not sure it was the one rule or the whole .htaccess file.

EDIT: I have added it and all still seems OK.


I think how it works is, you have a load of conditions and rules, the conditions will apply to all those rules, until you mark the last [L] one.
Then you can have more conditions and rules which are not bound by the previous conditions.


That makes sense now looking at the rest of the .htaccess file


RewriteCond's do not overwrite each other. They also apply to only the very next RewriteRule. They won't affect subsequent rules, regardless if you use [L] or not.

[L] means "Stop the rewriting process immediately and don't apply any more rules," though it often won't play out quite that simply. Let's say, for example, that you have:

RewriteRule foo bar [L]
RewriteRule bar baz

If we send a request for "foo", the first rule matches and changes "foo" to "bar". The second rule would have then changed "bar" to "baz", except the first rule's [L] stopped the rewriting process. ...But that's now where things end. Now that the request has been rewritten from "foo" to "bar", Apache sends an internal request for "bar". It goes through the normal request process, including applying rewrite rules. This time the first rule doesn't match, since the request is for "bar", not "foo", and the [L] doesn't apply either. The second rule does match, and "bar" is rewritten to "baz". Apache sends another internal request for "baz", and again it goes through the normal request process, including applying rewrite rules, but this time none of the rules match, so "baz" gets served up.


Thank you for the explanation @Jeff_Mott


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.