Question

I have a problem with the mod_rewrite module with Lighty.

I am trying to make this: example.com/index.php?search=whatever, appear as example.com/whatever or example.com/search/whatever (have not decided yet -- don't know much about SEO)

While I want it to act like above, I also want to exclude all physichal directories and all files, such as the directory /images/ and the files index.php, favicon.ico, style.css etc. from the rewrite, because it acts weird.

How would I achieve this? I've tried the following, which worked okay for what I wanted, but didn't really work with the exclusion of the directories and files:

url.rewrite-once = (
"^/([a-zA-Z0-9_-]+)" => "/index.php?search=$1",
"^/(images|js|wp-content)/(.*)" => "$0",
"(.*\.php|.*\.css|favicon.ico)" => "$0" )

By the way, what difference is there between this:

"^/([a-zA-Z0-9_-]+)" => "/index.php?search=$1",

And this:

"^/(.*)$" => "/index.php?search=$1"
Was it helpful?

Solution

To avoid having to add numerous RewriteCond directives checking that the visitor's request is not an actual file rather than an artificial path, I recommend going with the /search/whatever pattern in favour of the /whatever pattern. Then, so long as you never create an actual directory called "search", you'll never need to check whether a path beginning with /search is an actual file path. So your RewriteRule becomes this simple:

RewriteRule ^/search/([a-zA-Z0-9_-]+)$ /index.php?search=$1

(I'm not familiar with Lighty, so I'm not sure how to translate this into a url.rewrite-once instruction, but this is such a simple rewrite that it should be straightforward.)

However, the visitor's browser will now think that they are viewing a page which is in a sub-directory called "search", so if you have any image elements or CSS files specified with relative paths (paths not anchored to the root directory) such as src="images/photo.jpg" or href="stylesheets/clean.css" then the browser will think those paths are relative to the "search" directory and will ask your web server for /search/images/photo.jpg and /search/stylesheets/clean.css respectively.

There are two ways to do this. The first is to change all page decoration (images, stylesheets, JavaScript) paths to absolute paths. That is, change the path so that it begins with a forward-slash which represents the root directory of the website. So your image path would need to be changed to src="/images/photo.jpg" and your stylesheet path to href="/stylesheets/clean.css". The forward slash at the start tells the web browser that the path starts at the site's root directory, so there is no ambiguity.

The second option is to create convoluted RewriteRules to redirect requests for images, stylesheets, script files, etc, to the correct directories. This tends to become ugly and fragile if you have a lot of media types in a lot of different directories and you need them to work from a lot of different sub-directories (virtual and/or otherwise).

Which option you choose depends on your requirements and preferences.

Regarding your question about the difference between [a-zA-Z0-9_-]+ and .* the first pattern only allows letters a to z (lowercase or uppercase), digits, underscores and hyphens. The second pattern allows any characters. For security and debugging reasons, it's usually better to use the pattern which limits characters to only those which should be allowed. So I'd go with the first of those patterns, adding additional permitted characters if necessary, rather than allow all characters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top