Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jan 2007
    Posts
    154
    Thanks
    52
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    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

  • #3
    Regular Coder
    Join Date
    Jan 2007
    Posts
    154
    Thanks
    52
    Thanked 0 Times in 0 Posts

    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]

  • #4
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    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

  • Users who have thanked DJCMBear for this post:

    thoford75 (05-10-2012)

  • #5
    Regular Coder
    Join Date
    Jan 2007
    Posts
    154
    Thanks
    52
    Thanked 0 Times in 0 Posts
    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

  • #6
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    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

  • Users who have thanked DJCMBear for this post:

    thoford75 (05-10-2012)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •