Rewriting Subdirectories

Discuss practical ways rearrange URLs using mod_rewrite.

Rewriting Subdirectories

Postby smbeans » Sat Apr 30, 2005 4:48 am

I would like to achieve the following:

http://www.mydomain.com - No change
or
http://mydomain.com - No change


http://foo.mydomain.com
to
index.php?option=com_webpage&username=foo

and

http://foo.mydomain.com/page1.htm
to
index.php?option=com_webpage&username=foo&page=1

Any help would be appreciated.
smbeans
 

Postby smbeans » Sat Apr 30, 2005 5:05 am

This is hard. Would it start like this?

RewriteCond %{HTTP_HOST} !^www\.mydomain\.com [NC]
RewriteCond %{HTTP_HOST} !^mydomain\.com [NC]
smbeans
 

Postby Caterham » Sun May 01, 2005 6:06 am

Code: Select all
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?mydomain\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.])\.mydomain\.com [NC]
RewriteRule ^(page([0-9]+)\.htm)?$ /index.php?option=com_webpage&username=%2&page=$2 [L]


Note, that you'll have a page= (empty), if no file was requested
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm

Postby smbeans » Sun May 01, 2005 5:40 pm

Note, that you'll have a page= (empty), if no file was requested


Thanks for your help. Having a blank page parameter is no problem. I'll just code the program to have it go to the main page if the parameter is empty.
smbeans
 

Postby Guest » Sun May 01, 2005 7:56 pm

Well I've just tested this on my webserver and unfortunately it doesn't work.

I just get sent over to the default website (there are several websites on this server).
Guest
 

Postby smbeans » Sun May 01, 2005 8:49 pm

BTW Caterham - I've crossed posted this over at Experts-exchange. A chance for someone to up up 500 points.
smbeans
 

Postby smbeans » Mon May 02, 2005 11:22 pm

Well Caterham came through with the answer over at Experts-Exchange. Here is the full solution:

<VirtualHost *>
ServerName shire-biz.com
ServerAlias www.shire-biz.com *.shire-biz.com
DocumentRoot /var/www/html/shirebiz
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?shire-biz\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.shire-biz\.com [NC]
RewriteRule ^/(page([0-9]+)\.htm)?$ /index.php?option=com_webpage&username=%2&page=$2 [L]
</VirtualHost>

Thank you for your help.
smbeans
 

Postby Guest » Tue May 24, 2005 11:48 pm

Hello again,

I discovered I need to add another couple of levels of difficulties to this one.

Firstly I will need to create a sub-domain:

http://forum.shire-biz.com which is to remain unchanged (similar to www).

I also need to make a change to the last line:

RewriteRule ^/(page([0-9]+)\.htm)?$ /index.php?option=com_webpage&username=%2&page=$2 [L]

so that the 'page' part can be changed to anything as represented by 'foo' here:


RewriteRule ^/(foo([0-9]+)\.htm)?$ /index.php?option=com_webpage&username=%2&page=foo_$2 [L]

Thanks
Leonie
Guest
 

Postby Caterham » Tue May 31, 2005 12:20 pm

Code: Select all
<VirtualHost *>
ServerName shire-biz.com
ServerAlias www.shire-biz.com *.shire-biz.com
DocumentRoot /var/www/html/shirebiz
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.|forum\.)?shire-biz\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.shire-biz\.com [NC]
RewriteRule ^/(([a-zA-Z0-9_-]+)([0-9]+)\.htm)?$ /index.php?option=com_webpage&username=%2&page=$2_$3 [L]
</VirtualHost>
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm

Postby smbeans » Fri Jun 03, 2005 10:19 pm

Thank you very much! It works a dream.

Cheers
Leonie
smbeans
 


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 46 guests

cron