URL Rewriting for a CMS

Discuss practical ways rearrange URLs using mod_rewrite.

URL Rewriting for a CMS

Postby Haran » Mon Feb 23, 2009 4:02 am

Hi guys,
I've spent hours reading threads on the forum but I'm probably too n00b to completely understand everything and be able to start writing rules from scratch so I'd appreciate your help :)

First of all I need to redirect different domains to the same website and this is the code I used in my .htaccess file:

Code: Select all
Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^haransite.com [NC]
RewriteRule ^(.*)$ http://www.haransite.com/$1 [R=301]

RewriteCond %{HTTP_HOST} ^haransite.net [NC]
RewriteRule ^(.*)$ http://www.haransite.com/$1 [R=301]

RewriteCond %{HTTP_HOST} ^www.haransite.net [NC]
RewriteRule ^(.*)$ http://www.haransite.com/$1 [R=301]


This code does work only in the homepage. How can I solve it?


Second problem, the actual CMS's structure cannot be modified, the only thing I can do is to append content, taken from the db, to the URLs.

Here are a couple of examples of rewriting I'd like to do:

http://www.haransite.com/public/index.cgi

to

http://www.haransite.com/

and

http://www.haransite.com/public/destina ... ETF4VG8RGH&loc=3DEVHHRXAENW

to

http://www.haransite.com/destinations/g ... TF4VG8RGH-3DEVHHRXAENW

(The text in orange doesn't always occur, just sometimes in the homepage links.)


There are other cgi scripts in the cms folders but the queries usually remain almost the same.


Here it is the main structure of my CMS:

/ (root)
/public
/admin
/cpanel

admin and cpanel will become subdomains (admin.haransite.com & cpanel.haransite.com) so the main site will be in public.

Could you please help me to start? I just need a hint, an example just to begin writing.

Since I've full control over the server, do you think I should place the rules in the .htaccess or inside the virtual-host config file?


Thanks,
Haran
Haran
 
Posts: 16
Joined: Sat Feb 21, 2009 1:09 pm

Postby richardk » Mon Feb 23, 2009 5:35 am

First of all I need to redirect different domains to the same website and this is the code I used in my .htaccess file:

Do you have any other mod_rewrite and/or .htaccess files?

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^haransite\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?haransite\.net$ [NC]
RewriteRule ^(.*)$ http://www.haransite.com/$1 [R=301,L]


Second problem, the actual CMS's structure cannot be modified, the only thing I can do is to append content, taken from the db, to the URLs.

You have to be able to change how the links are outputted by the script.

Try
Code: Select all
RewriteRule ^$ /public/index.cgi [QSA,L]
RewriteRule ^destinations/([a-z]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([a-z0-9]+)(-([a-z0-9]+))?$ /public/destination.cgi?cat=$1&area1=$2&area2=$3&area3=$4&des=$5&cdo=$6&loc=$8 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Haran » Mon Feb 23, 2009 11:05 am

Hi richardk,
thanks a lot for your help :)
In the second rule, the one with the query string I get a 404 error, the server looks for "destinations" in the site root.

What do you think about using {QUERY_STRING}?


Haran
Haran
 
Posts: 16
Joined: Sat Feb 21, 2009 1:09 pm

Postby richardk » Tue Feb 24, 2009 2:19 am

In the second rule, the one with the query string I get a 404 error, the server looks for "destinations" in the site root.

What URL are you visiting?
Does this rule work?
Code: Select all
RewriteRule ^$ /public/index.cgi [QSA,L]


What do you think about using {QUERY_STRING}?

What for?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Haran » Wed Feb 25, 2009 2:22 pm

Hi Rickard,
the first rule works like a charm, thanks :)
The second one can't work because I was not specific enough about how the system works.
The real url is not the following:
http://www.haransite.com/public/destina ... EVHHRXAENW

the real url is:

http://www.haransite.com/public/destination.cgi?cat=%-product.catalogue-%&area1=%-product.locarea1des-%&area2=%-product.locarea2des-%&area3=%-product.locarea3des-%&des=%-product.locationdes-%&cdo=%-product.repuri-%&loc=%-product.locationcode-%

where the parts within the "%- -%" are variables the CMS replace with the values you saw in the first url.

Do you think it's possible to write a rule?


Haran

P.S.
Forget about the query-string, I was a bit confused on how to use it.
Haran
 
Posts: 16
Joined: Sat Feb 21, 2009 1:09 pm

Postby richardk » Thu Feb 26, 2009 7:03 am

With the second rule, what happens when you visit /destinations/guide/ITALY/Campania/Capri-Island/Capri/001B46ETF4VG8RGH-3DEVHHRXAENW?

the real url is:

/public/destination.cgi?cat=%-product.catalogue-%&area1=%-product.locarea1des-%&area2=%-product.locarea2des-%&area3=%-product.locarea3des-%&des=%-product.locationdes-%&cdo=%-product.repuri-%&loc=%-product.locationcode-%

You would need to change that to
Code: Select all
/destinations/%-product.catalogue-%/%-product.locarea1des-%/%-product.locarea2des-%/%-product.locarea3des-%/%-product.locationdes-%/%-product.repuri-%-%-product.locationcode-%

with one small chnage to the rule, replace
Code: Select all
(-([a-z0-9]+))?

with
Code: Select all
(-([a-z0-9]*))?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Haran » Thu Feb 26, 2009 11:10 am

Ok, I've made some progress.


RewriteRule ^/$ /public/index.cgi [QSA,L] (to handle the site root)
RewriteRule ^/([a-z_-]+)\.cgi$ /public/$1.cgi [QSA,L] (to handle all other scripts)

Those 2 work perfectly but the following one doesn't

RewriteRule ^/([a-z-_]+)/([a-z]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([a-z0-9]+)(-([a-z0-9]*))?$ /public/$1.cgi?cat=$2&area1=$3&area2=$4&area3=$5&des=$6&cdo=$7&loc=$9 [QSA,L]

The problem is, how can I obtain the variables values? %-product.catalogue-% value is "guide", how can I retrieve it?

Shouldn't I define somewhere all the system variables I need to use?

Here is the error I get right now:
File does not exist: /home/haran/web/destination (/home/haran/web/ is the site root)


Haran
Haran
 
Posts: 16
Joined: Sat Feb 21, 2009 1:09 pm

Postby richardk » Sun Mar 01, 2009 7:27 am

Mod_rewrite has no knowledge of "system variables". It gets passed the URL (eg. /abc.cgi) from the address bar of the browser. Then it checks it against the RewriteRule regular expressions (eg. ^/([a-z_-]+)\.cgi$) (and RewriteConds) and if there is a match a substitution is carried out (eg. in /public/$1.cgi the $1 is replaced by "abc").

Here is the error I get right now:
File does not exist: /home/haran/web/destination (/home/haran/web/ is the site root)

This regular expression
Code: Select all
^/([a-z-_]+)/([a-z]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([a-z0-9]+)(-([a-z0-9]*))?$

only matches when all variables, except the last one which is optional, are present. For example /destinations/guide/ITALY/Campania/Capri-Island/Capri/001B46ETF4VG8RGH and /destinations/guide/ITALY/Campania/Capri-Island/Capri/001B46ETF4VG8RGH-3DEVHHRXAENW.

If you want it to match with only some of the variables you will need seperate rules
Code: Select all
RewriteRule ^/([a-z-_]+)/([a-z]+)/?$ /public/$1.cgi?cat=$2 [QSA,L]
RewriteRule ^/([a-z-_]+)/([a-z]+)/([^/]+)/?$ /public/$1.cgi?cat=$2&area1=$3 [QSA,L]
# etc.

or you can try this rule
Code: Select all
RewriteRule ^/([a-z-_]+)(?:/([a-z]+)(?:/([^/]+)(?:/([^/]+)(?:/([^/]+)(?:/([^/]+)(?:/([a-z0-9]+)(?:-([a-z0-9]*))?)?)?)?)?)?)?/?$ /public/$1.cgi?cat=$2&area1=$3&area2=$4&area3=$5&des=$6&cdo=$7&loc=$8 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Haran » Mon Mar 09, 2009 8:12 am

Hi richardk,
I was able to make it almost work using this rule in the .htaccess file:

RewriteRule ^destination/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ /public/destination.cgi?cat=$1&area1=$2&area2=$3&area3=$4&des=$5&cdo=$6&loc=$7 [L]

The server correctly interpret the rule and find the cgi script.
The main problem I got now is related to the graphics, because the CGI script use the rewritten url to find the template and the graphics and append the path to graphics to the current page url.

http://www.haransite.com/destinations/g ... s/logo.gif

Is there a way to rewrite/redirect every url/link containing www/public to the real path? (/www/public/...)
Can this rule be dynamic? With dynamic I mean not related to the number of subdir in the rewritten url?



Thanks,
Haran
Haran
 
Posts: 16
Joined: Sat Feb 21, 2009 1:09 pm

Postby richardk » Tue Mar 10, 2009 12:43 pm

richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Next

Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 22 guests

cron