Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite and .htaccess

         

cnapsys

11:15 pm on Aug 8, 2011 (gmt 0)

10+ Year Member



I need to write a few rules for the new site I'm working on.
So far it looks like there's at least 3 separate rules that need to be implemented.

1) rewrite the following:
mydomain.com/browse.php?cat=2
into:
mydomain.com/browse/2/

2) rewrite:
mydomain.com/image.php?id=44
into:
mydomain.com/image/44/

3) rewrite any other php file
mydomain.com/contact.php
into:
mydomain.com/contact/

so, I was able to write the first one just fine:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^browse/([0-9]+)/$ browse.php?cat=$1


and it works just fine.

I'm assuming the 2nd one shouldn't be any different and shouldn't interfere with the 1st in any way... but can't figure how to write the 3rd and make sure they do not butt heads or overwrite each other.

Any help is greatly appreciated...
Thank you for your time

g1smd

11:26 pm on Aug 8, 2011 (gmt 0)

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



Mod_rewrite doesn't "make" new URLs. It doesn't rewrite internal filepaths into external URLs.

It works exactly the opposite way. You create the SEF links on your pages and mod_rewrite looks for specific patterns in the incoming URL requests. When a pattern matches, that rule is run and it specifies the internal filepath to fetch the content from. That's the rewrite, changing where on the harddrive the server will look for the content.

Add the [L] flag to every rule. When a rule matches, the [L] flag ensures that after the rule is processed, all other mod_rewrite processing is halted for this request and control is handed to the content delivery functions to deliver that content.

Another thing to remember is that you should list rewrites in order from most selective (affects the least number of requests) to the most general (affects the most number of requests). The final rewrite should pick up all the remaining valid requests that all of the previous rewrites have missed.

cnapsys

11:57 pm on Aug 8, 2011 (gmt 0)

10+ Year Member



Thanks for clarifying.
It does make sense... now that I think about it.

This look ok to you?

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^browse/([0-9]+)/$ browse.php?cat=$1 [L]
RewriteRule ^image/([0-9]+)/$ image.php?id=$1 [L]
RewriteRule ^(.*)/$ $1.php [L]

did a quick test and seems to be working fine:
mydomain.com/browse/2/ loads the info from browse.php?cat=2
mydomain.com/image/44/ loads the info from image.php?id=44
mydomain.com/about/ loads the info from about.php

Would there be anything else I need to pay attention to?

g1smd

12:45 am on Aug 9, 2011 (gmt 0)

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



Yep. That's just about nailed it.

Personally I would not use URLs ending with a slash. Those are reserved for "folders" and for "the index page in a folder".

I would use extensionless URLs. However, you would need to change the (.*) pattern to be something else otherwise requests for images, and for CSS and js files would be rewritten to your PHP script. I use
^[a-z0-9]+$
or
^[^/.]+$
or
^([^/]+/)*[^/.]+$
as appropriate.

One other thing your PHP scripts must do, is return the proper 404 headers and HTML error message when there is no content available for the current request.