Apache Mod Rewrite

Associate
Joined
13 Nov 2003
Posts
1,567
Location
Manchester
The client doesn't know any difference. With mod_rewrite, the rewriting is done server side. So if I have a rule that says "Rewrite mydomain.com/foo with mydomain.com/cgi-bin/index.pl?command=foo", and I then navigate to mydomain.com/foo, I just send a request and get data back. If you put a file "foo" inside your root dir, expecting that mydomain.com/foo will fetch it, the rewrite rule will happen first and tell the server to actually serve /cgi-bin/index.pl?command=foo.

I'm not sure what you mean when you say "access it directly through a search engine"? Your webserver wont serve up different content depending on what the referrer is. It doesn't know or care whether you access the url by typing it into the address bar, link it from google, or link it from a different part of your site. It sees a request and serves the content.

Hope that helps some.

*edit, in your case the rule would be:

Code:
RewriteRule ^news/([0-9]{1,}/?$ /newsthingiewhatever.php?id=$1

This means, take the beginning of the string to match (the bit after "http://www.domain.com/") - that's the ^ - then match a string that's "news/" followed by at least 1 totally numeric string followed optionally by a trailing slash and then the end of the url. replace that with the newsthingiewhatever.php?id=$1 with the $1 being whatever was found as the numbery bit.

This is very specific and will match /news/3/ and /news/28394 but not /news/398593859d/ or /news/35/f. If you request something that isn't matched, the server will go away and look for a "news" directory and then a directory inside that called 398593859d. And it probably won't find it.
 
Last edited:
Hmm that has given me a rather tasty internal server error lol

Code:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^news/([0-9]{1,}/?$ /show_news_article.php?id=$1
 
you have to turn it on, use google to find out. I wanted sumthing similar to what you want and this currently works:


RewriteEngine On
RewriteBase /

RewriteRule ^news/?$ index.php?p=news
RewriteRule ^news/([0-9]+)/?$ index.php?p=news&id=$1
RewriteRule ^articles/?$ index.php?p=articles
RewriteRule ^articles/([0-9]+)/?$ index.php?p=articles&id=$1
 
oh right lol

Having a small problem because I am working on my test server, so the url is http://www.fluiduk.co.uk/~dimensio/news/1

So its trying to access show_news_article.php at www.fluiduk.co.uk, not www.fluiduk.co.uk/~dimensio

Would whacking it on a sub domain help, as I dont want to have to rewrite all the rules when I stick on the live server

Aaron
EDIT - DIDNT WORK. As I cant redirect the subdomain totally, its just redirect to fluiduk.co.uk/~dimensio
 
Last edited:
You have to change the

Rewrite Base /

so if your stuff is in /mysite/ then change it to that.
 
so like this

Code:
Options +FollowSymLinks
RewriteEngine on
ReWrite Base /
RewriteRule news/([0-9]{1,})/?$ /show_news_article.php?id=$1

Thanks
 
{1,} can be shortened to +, fyi.

Code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule news/([0-9]+)/?$ show_news_article.php?id=$1

RewriteBase / is implicit too, iirc.

Edit: you'll need to maintain two .htaccess files, one with a RewriteBase of / and one with a RewriteBase of whatever the directory is on the other server.
 
{1,} for me is easier to remember what it means :p

+ is 1 or more (greedy)
* is 0 or more (greedy)
? is 0 or 1

This correct?
 
growse said:
{1,} for me is easier to remember what it means :p

+ is 1 or more (greedy)
* is 0 or more (greedy)
? is 0 or 1

This correct?

Yep, that's right.

I tend to favour terseness over explicitness--especially with something as ugly as regular expressions--but I can see why people do the opposite.
 
Ok guys that all went over my head. Here is another challenge for you.

I have another dynamic page that uses the following url view_facilities.php?id=gym&photoid=1

The reason I am using 2 variables is because I want the word gym to be in the url, and the photo ID is so the page can call the relevant photos the user has uploaded for that page.

I would like the url to look like www.mydomain.com/facilities/gym

Is that possible, given that there is another variable in the URL?

Much Appreciated
Aaron
 
Indeed, just put as many (...) as you like in the regexp, and match them with $1, $2 etc.

Eg:

Code:
^section/([^/]+)/([0-9]+)/?$ section.php?name=$1&id=$2

The first matches a series of one or more characters not including "/", and the second matches any series of one of more numbers. then it whacks them in as args to the section.php script.
 
Nice one, got all that working. Thanks

Is it worth while extending this practice to every page, how would I rewrite say careers.php to mydomain.com/careers and is it worth it?

Thanks
Aaron
 
Code:
RewriteRule ^/careers/?$  /careers.php [NC]
Yes it is good practice for future proofing. It stops your site structure being reliant on the technology employed at the time. You might be using PHP now, but in 6 months you could be using a different technology or method of accessing that particular resource e.g. you might change to /index.php?view=careers. Meaning you'll have to break all exisiting inbound links (bookmarks, search-engines, printed material etc.) to accomodate a new extension or address.

For the little effort required, and the tiny overhead, it could prove to be a huge timesaver later on.
 
That was my thinking

Thanks for all the help guys, im off to bed lol

I will no doubt be back to pester you all tommorrow ;)

Aaron
 
Quick question with this

Is there a way to do this rewrite that doesnt involve having to add ../ to all images, css, includes etc.

Perhaps its good practice to do this ../ anyway?
 
Cool, so just set all the image urls etc to start with / should do the trick.

Does this apply to page links?

Thanks
 
Back
Top Bottom