Forum Moderators: phranque
/books/fiction to /index.php?cat=books&page=fiction
/offers to /offers.php
/films/comedy to /index.php?cat=films$page=comedy
I have tried with limited success, with some of the rules I put in, the images and CSS files would no longer work (as in not included in the html file).
I am starting to think that this is not possible without me adding another paramater i.e.
/content/books/fiction to /index.php?cat=books&page=fiction
/content/films/comedy to /index.php?cat=films&page=comedy
If it is possible to not do this that is preffered, but the more I read the more this seems unlikely.
It should be noted that I am complete mod_rewrite virgin, but have read many tutorials and I think I am suffering from an information overload at the minute.
One article that I read skirted around the subject of changing the server variables, so would it be possible to change php's $_SERVER['DOCUMENT_ROOT']?
Thanks for your time.
Welcome to WebmasterWorld.
First I recommend visiting the Apache Library [webmasterworld.com] and Forum Charter [webmasterworld.com] to get a better understanding of what mod_rewrite does -- not sure what tutorials you have read, but there are some in there too... mainly basics in the library.
You should be able to do what you are asking with a single rule, which will look something like this:
RewriteRule ^content/([^/]+)/([^.]+)$ /index.php?cat=$1&page=$2
This says if the URL starts with "content", followed by a /, followed by one or more characters that is not a /, followed by another /, followed by one or more characters that is not a dot -- then -- serve the information from index.php?cat=the-first-set-of-()&page=the-second-set-of-().
Hope this helps.
Justin
I think I have cracked this example, but I still don't really know what the hell is going off.
What I have at the minute is:
RewriteEngine On
RewriteRule ^articles/([^.]+)$ /index.php?page=$1
RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?page=$1
How I understand it is,
look for articles followed by / (therefore a directory) then look for anything else after the / as long it is not a . (a page). If found then send to $index.php?page=(the contents of the 2nd line brackets)
If not found then do the next rule, (I got this rule off of an example website, I am not entirely sure what it does, it kinda just works for the first directory, but not for the next sub dir)
I have kinda changed the goal posts a little, what I am after now is:
/articles to /index.php?page=articles
/somethingelse to /index.php?page=somethingelse
but also
/articles/thepage to /index.php?page=thepage
/somethingelse/anotherpage to /index.php?page=anotherpage
The rules I have at the minute work for /articles but that is it (I realise that is the one I choose) but when I try this instead:
RewriteRule ^/(^/)/([^.]+)$ /index.php?page=$1
RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?page=$1
it doesn't work.
with regards to the $_SERVER['DOCUMENT_ROOT'] thing, the mod_rewrite manual says something about it, but is over my head, the most logical way I can think of doing it is: (which is obviousley wrong)
ReWriteCond DOCUMENT_ROOT /home/myweb
Anyway, thanks a lot again justin
Is this normal and just one of the things you have to put up with using mod_rewrite? or is it something else unrelated to mod_rewrite, if so any ideas?
<snip>
[edited by: jdMorgan at 8:15 pm (utc) on Oct. 31, 2005]
[edit reason] No URLs, please. See TOS. [/edit]
First: mod_rewrite does not say anything about $_SERVER['anything'] because that is php, not mod_rewrite
Second: There are a number of ways you to end up at the solution you are looking for... the most effective for you will depend on *exactly* what you need.
For instance to rewrite /articles/ and /content/ you could use any of the following depending on your situation:
If you have other similar directory or page names, you can use a specific rewrite.
RewriteRule ^(articles¦content)/([^.]+)$ /index.php?page=$2
If they are the only directories you have that start with a OR c, and you do not want to use a 'catch-all' you can use the first letter, followed by any other character that is not a /, etc...
RewriteRule ^(a¦c)[^/]+/([^.]+)$ /index.php?page=$2
If you only have the two directories (or all directories should be rewritten), you can use a 'catch-all'
RewriteRule ^[^/]+/([^.]+)$ /index.php?page=$1
You should be able to find a good starting point in one of the above examples for what you are trying to do... If you get stuck again, let us know.
I have not used either of the sites you mentioned, so I cannot comment on Search Engines, except that there are only a few that really matter and they do not have problems with sites that use mod_rewrite correctly.
Hope this helps.
Justin
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?page=$1
RewriteRule ^[^/]+/([^.]+)$ /index.php?page=$1
which still lets me have
/whatever
/whatever.php
/whatever/somethingElse
Apologies for posting the links, I did not realise.
Cheers again