RewriteRule changes address in user address bar

Oh, the strange things mod_rewrite does!

RewriteRule changes address in user address bar

Postby longears » Fri Jul 27, 2007 1:53 pm

here's my rewrite block:

Code: Select all
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.*
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com
RewriteRule ^ http://www.domain.com/profiles.php?subdomain=%1 [L]


This used to work on a server running apache 1.x (at least, I think it was 1.x), but one we changed servers to a debian build, running apache2, it broke. Allow me to illustrate:

Previously:
user types in: longears.domain.com
user views page: www.domain.com/profiles.php?subdomain=longears
user sees in address bar: http://longears.domain.dom

Now:
user types in: longears.domain.com
user views page: www.domain.com/profiles.php?subdomain=longears
user sees in address bar: http://www.domain.com/profiles.php?subdomain=longears

As you can see, instead of the user seeing what they typed in, they are seeing what the rewrite rule rewrites.

The rule is working fine, but it's rewriting the address in the address bar. Normally, I'd be fine with this, but we've got search engine ranking that we don't want to loose.

I thank all in advance for their help.

db
longears
 
Posts: 12
Joined: Fri Jul 27, 2007 1:39 pm

Postby richardk » Fri Jul 27, 2007 2:53 pm

You shouldn't include the domain in the RewriteRule. If Apache does not know if the domain is the same as the current <VirtualHost> or server (through other not mod_rewrite configuration or a change in code because of a version upgrade) it has to redirect.

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
RewriteRule !^profiles\.php$ /profiles.php?subdomain=%1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby longears » Fri Jul 27, 2007 3:05 pm

That worked wonderfully.. only now all of our links are broken: stylesheets, images, etc.

I figured that since you nailed my problem the first try, you may have some insight on this as well..

THANK YOU!!!
longears
 
Posts: 12
Joined: Fri Jul 27, 2007 1:39 pm

Postby richardk » Fri Jul 27, 2007 3:23 pm

They're probably getting rewritten too.

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
# If it's not a real file.
RewriteCond %{SCRIPT_FILENAME} !-f
# If it's not a real directory.
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule !^profiles\.php$ /profiles.php?subdomain=%1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby longears » Sat Jul 28, 2007 8:59 am

The two lines you sent me didn't work, so I made three exeptions, and it worked perfectly. Do you know how I can modify this so I don't have to make exceptions?

Code: Select all
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domainl\.com$ [NC]
RewriteCond %{HTTP_HOST} !^blog\.domain\.com$ [NC]
RewriteCond %{SCRIPT_FILENAME} !^/uploads/*
RewriteCond %{SCRIPT_FILENAME} !^/style/*
RewriteCond %{SCRIPT_FILENAME} !^/images/*
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]

# If it's not a real file.
RewriteCond %{SCRIPT_FILENAME} !-f
# If it's not a real directory.
RewriteCond %{SCRIPT_FILENAME} !-d

RewriteRule !^profiles\.php$ /profiles.php?subdomain=%1 [QSA,L]


Again, thanks for all your help. You saved my bacon.

db
longears
 
Posts: 12
Joined: Fri Jul 27, 2007 1:39 pm

Postby richardk » Sat Aug 04, 2007 12:10 pm

This is shorter
Code: Select all
Options +FollowSymlinks

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www|blog)\.domainl\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule !^(profiles\.php|(uploads|style|images)(/.*)?)$ /profiles.php?subdomain=%1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby edmund » Mon Dec 03, 2007 12:54 am

Just had the same problem. Removed URL part and it worked.

But there's still a minor problem.

I have two way to access my site:
- http://something.blog.domain.com
- http://domain.com/blog/something

And both should point to http://domain.com/blog.php?b=something

This is how i did it now.
Code: Select all
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([a-z]{3,})\.blog\.domain\.com$ [NC]
RewriteRule ^$ blog.php?b=%1 [L]
RewriteRule ^blog/([a-z]{3,})(/)?$ blog.php?b=$1 [L]


Can it be done so the first RewriteRule redirects to http://domain.com/blog/something and then that rule is matched again? If i just apply [R] after first rule, then it breaks totally and uses the whole webroot path for the $1.
I want the http://domain.com/blog/something URL displayed in the address bar and use http://something.blog.domain.com as forward only.
edmund
 
Posts: 2
Joined: Mon Dec 03, 2007 12:40 am

Postby richardk » Mon Dec 03, 2007 9:16 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^([a-z]{3,})\.blog\.domain\.com$ [NC]
RewriteRule ^$ /blog/%1/? [R=301,L]

RewriteRule ^blog/([a-z]{3,})/?$ /blog.php?b=$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby edmund » Tue Dec 04, 2007 11:29 pm

Thanks, works now. Just had to include the host to the first rule, else i get problems with session cookie host.

Code: Select all
RewriteCond %{HTTP_HOST} ^([a-z]{3,})\.blog\.domain\.com$ [NC]
RewriteRule ^$ http:/domain.com/blog/%1/? [R=301,L]
RewriteRule ^blog/([a-z]{3,})/?$ /blog.php?b=$1 [QSA,L]
edmund
 
Posts: 2
Joined: Mon Dec 03, 2007 12:40 am

Postby sam9 » Wed Dec 12, 2007 6:48 am

I have several applications running on tomcat, which I direct to apache virtual hosts and each one has its own domain name.

I want to set up rule(s) to get this:

user types: domain1.com
user gets: domain1.com/main/home.do?d=uk
but user sees: domain1.com


I tried this:

Code: Select all
RewriteEngine On

RewriteCond %{HTTP_HOST} ^([a-z]{3,})\.domain1\.com$ [NC]
RewriteRule ^$ /core/es/? [R=301,L]

RewriteRule ^core/([a-z]{3,})/?$ /core/home.do?d=$1 [QSA,L]



Any suggestions?
sam9
 
Posts: 7
Joined: Thu Oct 25, 2007 12:53 am

Next

Return to Idiosyncrasies

Who is online

Users browsing this forum: No registered users and 2 guests

cron