multiple rules

Discuss practical ways rearrange URLs using mod_rewrite.

multiple rules

Postby Neeon » Thu Sep 25, 2008 4:47 am

I've got a tricky question, I have downloaded the mod_Rewrite cheatsheet, but with no luck of finding my answer..

is there a way of transforming
index.php?s=sitename&lng=en_us&node=about_us

to:
domain.com/sitename/en_us/about_us
or:
domain.com/about_us (if "s" and "lng" has the default value..?)

And one other thing, some pages uses other strings, like
- q
- cat
- page
- p

etc..

Is it possible to let those go as normal
i.e:
domain.com/sitename/en_us/about_us?q=search+term&page=11&p=22

I'm sorry if this seems like, "I have a problem, someone please solve it for me" kinda question, but that's NOT what i'm asking, i'm just want some pointers to how i can solve it...
Neeon
 
Posts: 10
Joined: Sun Apr 29, 2007 6:25 am

Postby richardk » Thu Sep 25, 2008 2:14 pm

is there a way of transforming
index.php?s=sitename&lng=en_us&node=about_us

to:
domain.com/sitename/en_us/about_us
or:
domain.com/about_us (if "s" and "lng" has the default value..?)

Using [^/]+ (or an even more limited [url]character class[/url] like [a-z]) to match anything between two slashes will allow you to match either 3 values (s, lng and node) or one value (node). You can add any default value to the second part of the RewriteRule.

And one other thing, some pages uses other strings, like
- q
- cat
- page
- p
etc..

Is it possible to let those go as normal
i.e:
domain.com/sitename/en_us/about_us?q=search+term&page=11&p=22

You want the QSA (Query String Append) flag.

Part of it
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# /about_us/ to /index.php?s=default&lng=default&node=about_us.
RewriteRule ^([^/]+)/?$ /index.php?s=default&lng=default&node=$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Neeon » Fri Sep 26, 2008 2:56 am

Ok, so this would be the correct way then?
Code: Select all
Options +FollowSymLinks

RewriteEngine On
# First rule, No language or site
RewriteRule ^([^/]+)/?$ /index.php?node=$1 [QSA,L]
# Second rule, Just language
RewriteRule ^([^/]+)/([^/\.]+)/?$ /index.php?lng=$1&node=$2 [QSA,L]
# Third rule, Just site
RewriteRule ^([^/]+)/([^/\.]+)/?$ /index.php?s=$1&node=$2 [QSA,L]
# Fourth rule, all of them
RewriteRule ^([^/]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?s=$1&lng=$2&node=$3 [QSA,L]
Neeon
 
Posts: 10
Joined: Sun Apr 29, 2007 6:25 am

Postby Neeon » Fri Sep 26, 2008 4:31 am

Ok, I've done some changes..
I havent got the time yet to test it, but i have an other question..
is it possible to make ".htaccess" load data from an other file, like
"mod_rewrite.php" ?? Line 3 will change alot, cuz i'm gonna use it on other
sites..
And one other thing, would creating a subdomain like this mess up the other rules so this would't work? : http://sitename.domain.no/lang/en_us/about_us

Code: Select all
# Reroute subdomain
RewriteCond %{HTTP_HOST} !^w{3}\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.no$ [NC]
RewriteRule ^.*$ index.php?site=%1 [L]
# Check language and page
RewriteRule ^lang/([^/]+)/([^/]+)/?$ /index.php?lng=$1&node=$2 [QSA,L]
# Just page
RewriteRule ^([^/]+)/?$ /index.php?node=$1 [QSA,L] 
Neeon
 
Posts: 10
Joined: Sun Apr 29, 2007 6:25 am

Postby richardk » Tue Sep 30, 2008 2:36 am

Code: Select all
# Second rule, Just language
RewriteRule ^([^/]+)/([^/\.]+)/?$ /index.php?lng=$1&node=$2 [QSA,L]
# Third rule, Just site
RewriteRule ^([^/]+)/([^/\.]+)/?$ /index.php?s=$1&node=$2 [QSA,L]

This will not work, the first rule will always match because the patterns are the same.

is it possible to make ".htaccess" load data from an other file, like
"mod_rewrite.php" ?

Only if you have access to the httpd.conf file to use a RewriteMap.

And one other thing, would creating a subdomain like this mess up the other rules so this would't work? : http://sitename.domain.no/lang/en_us/about_us

You can do it, but i don't think your rules will work (i haven't tested though). Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Get the site name and put it in %{ENV:SITE_NAME}.
RewriteCond %{HTTP_HOST} !^www\.example\.no$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.no$ [NC]
RewriteRule .* - [E=SITE_NAME:%1]

RewriteCond %{ENV:SITE_NAME} ^(.+)$
RewriteRule ^$ index.php?site=%1 [L]

RewriteCond %{ENV:SITE_NAME} ^(.+)$
RewriteRule ^([^/]+)/?$ /index.php?site=%1&node=$1 [QSA,L]

RewriteCond %{ENV:SITE_NAME} ^(.+)$
RewriteRule ^lang/([^/]+)/([^/]+)/?$ /index.php?site=%1&lng=$1&node=$2 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: Google [Bot] and 19 guests

cron