Forum Moderators: phranque

Message Too Old, No Replies

Keep URL in Rewrite.

         

vitorzaninotto

7:00 pm on May 30, 2008 (gmt 0)

10+ Year Member



I´m doing a simple Rewrite to redirect my subdomains to a PHP page. It works well, but the URL in browser changes from subdomain.example.com to http://www.example.com/page.php.
I want to keep the url subdomain.example.com.com in my browser.

My htaccess code is:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/page.php?url=$1 [L]

Thanks

[edited by: jdMorgan at 7:43 pm (utc) on May 30, 2008]
[edit reason] example.com [/edit]

jdMorgan

7:46 pm on May 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have mixed two functions here, one which should be an external redirect, and the other which should be an internal rewrite. You need to split these functions. You are also invoking a 302 temporary redirect -- a very bad idea. To fix all this:

RewriteEngine on
#
# Externally redirect to canonical domain
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Internally rewrite friendly URLs to page.php script
RewriteCond $1 !^page\.php$
RewriteRule (.*) /page.php?url=$1 [L]

You may want to add more RewriteCond exceptions to the second rule, as shown here for "page.php" (which prevents rewrite looping). For example, you may wish to NOT rewrite requests for /robots.txt, /.htaccess, /sitemap.xml, /w3c/p3p.xml, /labels.rdf, image/media/pdf files, JavaScripts, or CSS stylesheets.

You may or may not wish to use the first rule at all -- Because you've mixed the two functions, I can't tell. The second rule will only work if your subdomains are in the same filespace on the same server as the files for yoour main domain. If they are on a different server or in a separate filespace, then there is no way to do an internal rewrite, and you'll either have to proxy the requests over, or use a redirect as you have done (which always changes the address bar).

Jim