Go Back   CodingForums.com > :: Server side development > Apache configuration

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-09-2012, 05:01 PM   PM User | #1
thoford75
Regular Coder

 
Join Date: Jan 2007
Posts: 154
Thanks: 52
Thanked 0 Times in 0 Posts
thoford75 is an unknown quantity at this point
Multiple Rewrites

Hi all. Trying to modify my htaccess file to do the following:

1) all subdomains (except www) redirect from foo.domain.com to domain.com/hosting/foo (I've setup wildcard dns)

2) any domain such as domain.com/about-us goes to domain.com/page.php (where it is processed)

3) domain.com/admin is an exception to number 2.

My file looks like this:
Code:
RewriteEngine on 
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteRule ^$ /%1 [L]

##### Do not process admin area #####
RewriteEngine on 
RewriteRule ^admin/?.*$ - [PT,L]

##### General redirect #####
RewriteEngine on 
RewriteRule ^([^/\.]+)/?$ page.php?url=$1 [QSA]
I had the code for the page.php part working fine until I moved domains and added the rewrite for the subdomain.

Really could do with some help here please!
thoford75 is offline   Reply With Quote
Old 05-10-2012, 03:42 PM   PM User | #2
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
Try this, un-tested but worth trying.

Code:
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^www\.domain\.com
    RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
    RewriteRule ^$ /%1 [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/\.]+)/?$ /page.php?url=$1 [NC,QSA]
</IfModule>
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Old 05-10-2012, 03:52 PM   PM User | #3
thoford75
Regular Coder

 
Join Date: Jan 2007
Posts: 154
Thanks: 52
Thanked 0 Times in 0 Posts
thoford75 is an unknown quantity at this point
update..

I have edited my .htaccess file to divert:

something.domain.com => domain.com/hosted/something

with the exception of www.domain.com.

Before I had this in place, I had a rule which would rewrite (for example)

domain.com/contact-us => domain.com/page.php?url=contact-us and show domain.com/contact-us

Now what I need is when I'm on "something.domain.com/contact-us" (for example), it to go to "domain.com/hosted/something/page.php?url=contact-us" and to show "something.domain.com/contact-us"

My current htaccess file:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteRule ^$ /hosted/%1 [L]

##### Do not process admin area #####
RewriteRule ^admin/?.*$ - [PT,L]

##### General redirect #####
RewriteRule ^([^/\.]+)/?$ page.php?url=$1 [QSA]
thoford75 is offline   Reply With Quote
Old 05-10-2012, 04:02 PM   PM User | #4
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
Ok well then,

Replace
Code:
RewriteRule ^$ /hosted/%1 [L]
with
Code:
RewriteRule ^(.*)$ /hosted/%1/$1 [L]
Then insert this in the general area
Code:
RewriteRule ^/hosted/([^/\.]+)/([^/\.]+)/?$ /hosted/$1/page.php?url=$2 [QSA]
Again un-tested so may not work.
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Users who have thanked DJCMBear for this post:
thoford75 (05-10-2012)
Old 05-10-2012, 04:29 PM   PM User | #5
thoford75
Regular Coder

 
Join Date: Jan 2007
Posts: 154
Thanks: 52
Thanked 0 Times in 0 Posts
thoford75 is an unknown quantity at this point
Thanks for the help, but I'm afraid it doesn't quite work :/

If I navigate to:

http://sub.domain.com/hosted/sub/pag...url=contact-us

the link works perfectly.

What I need to happen is that the URL is rewritten to look like:

http://sub.domain.com/contact-us

and if the user types in http://sub.domain.com/contact-us there are taken to http://sub.domain.com/hosted/sub/pag...url=contact-us but with the url displaying http://sub.domain.com/contact-us
thoford75 is offline   Reply With Quote
Old 05-10-2012, 04:38 PM   PM User | #6
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
I will do some more work on it when I get home from work in about half an hour, until then I hope you find out the issue and fix it
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Users who have thanked DJCMBear for this post:
thoford75 (05-10-2012)
Reply

Bookmarks

Tags
directory, htaccess, subdomain

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:08 AM.


Advertisement
Log in to turn off these ads.