.htaccess redirect help

New to mod_rewrite? This is a good place to start.

.htaccess redirect help

Postby alinux » Tue Jun 16, 2009 10:39 am

Hi
I am using the following .htaccess file:

rewriteengine on
rewritecond %{request_uri} !^/index.php
rewriterule ^. /index.php [R,L]


This causes the following:

www.abc.com/index.php --goes to--> www.mylocalserver.com/index.php "Address bar shows www.abc.com/index.php"
www.bcd.com/test.php --goes to --> www.mylocalserver.com/index.php "address bar www.bcd.com/index.php"

I need to change this to work as follows

www.abc.com/index.php --goes to--> www.mylocalserver.com/index.php?URL=www ... /index.php "Address bar shows www.mylocalserver.com/index.php"
www.bcd.com/test.php --goes to --> www.mylocalserver.com/index.php?URL=www ... m/test.php "Address bar shows www.mylocalserver.com/index.php"

thanks
alinux
 
Posts: 8
Joined: Tue Jun 16, 2009 5:15 am

Postby richardk » Tue Jun 16, 2009 12:48 pm


You can have "Address bar shows www.abc.com/index.php"
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^(www\.)?mylocalserver\.com$ [NC]
RewriteRule .* /index.php?URL=%{HTTP_HOST}%{REQUEST_URI} [QSA,L]


Or "Address bar shows www.mylocalserver.com/index.php?URL=www ... /index.php"
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^(www\.)?mylocalserver\.com$ [NC]
RewriteRule .* http://www.mylocalserver.com/index.php?URL=%{HTTP_HOST}%{REQUEST_URI} [QSA,L]


but not "Address bar shows www.mylocalserver.com/index.php" because from that URL there's no reliable way for mod_rewrite to know which domain you have visited.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby alinux » Wed Jun 17, 2009 3:20 am

Hi
I did apply the recommended settings. At this point my question has been answered, and the page does behave like I have initially requested.However, I am still facing the problem that I did post this question for.

The original problem was.

The once a request came in the users was redirected to a page where he agrees to a policy and when he accepts he gets redirected to the page he initially requested.

The problem is that the redirect is always going to the pause/block/initial redirect index.php page.

Example scenario

User requests www.abc.com/test.php
His requests is read by mod_rewrite
He gets redirected to www.mylocalhost.com/index.php?url=http: ... m/test.php

After displaying a short policy notice, he gets redirected to http://www.abc.com/test.php. However the browser immediately displays. www.mylocalhost.com/index.php?url=http: ... m/test.php instead of displaying http://www.abc.com/test.php

As if the page itself was cached.

This is kind of a captive portal on my network, where users traffic is intercepted and once they agree to the network traffic policy their Internet is unlocked and they are redirected to the page they initially requested.
alinux
 
Posts: 8
Joined: Tue Jun 16, 2009 5:15 am

Postby richardk » Wed Jun 17, 2009 11:21 am

To stop the mod_rewrite matching every request, even after the policy has been accepted, you would need to set a cookie or URL variable so the mod_rewrite would know the policy had been accepted.

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
# If the policy cookie is not set.
RewriteCond %{HTTP_COOKIE} !^(.+;\ )?cookie_name= [NC]
# send the request to the policy page.
RewriteRule .* /index.php?URL=%{HTTP_HOST}%{REQUEST_URI} [QSA,L]


Or
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# If the policy cookie is not set.
RewriteCond %{HTTP_COOKIE} !^(.+;\ )?cookie_name= [NC]
RewriteCond %{HTTP_HOST} !^(www\.)?mylocalserver\.com$ [NC]
# send the request to the policy page.
RewriteRule .* http://www.mylocalserver.com/index.php?URL=%{HTTP_HOST}%{REQUEST_URI} [R,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 99 guests

cron