RewriteRule when the variable is an URL

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

RewriteRule when the variable is an URL

Postby dblanco » Fri May 08, 2009 1:53 pm

Hi,

how to make a RewriteRule when the variable is a URL?

example:

http://www.mydomain.com/redirect.php?ur ... google.com

i want to convert to:

http://www.mydomain.com/url/http://www.google.com

thanks
dblanco
 
Posts: 10
Joined: Wed Apr 08, 2009 1:08 am

Postby richardk » Sat May 09, 2009 11:03 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^url/http:/(.+)$ /redirect.php?url=http://$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby dblanco » Sat May 09, 2009 1:05 pm

brilliant !!! it's perfect.

I never would have happened to place only 1 / character at http:/(.+)$. I do not understand how it works, but obviously it's the way.


Another question. In this same mod_rewrite I use use this rule:

Code: Select all
RewriteRule ^code/([^/]+)/?$ get_link.php?pl=$1 [NC,L]


It transforms this: http://www.mydomain.com/get_link.php?pl=xyzxyzxyz

in: http://www.mydomain.com/code/xyzxyzxyz/

but really I want to get: http://www.mydomain.com/xyzxyzxyz/

I tried something like:

Code: Select all
RewriteRule ^([^/]+)/?$ get_link.php?pl=$1 [NC,L]


but don't works Ok, and produces some problems with my index.php page, that obviously always keep invisible (like I want), even when using an anchor http://www.mydomain.com/#faq

Thanks again master,

DaViD
dblanco
 
Posts: 10
Joined: Wed Apr 08, 2009 1:08 am

Postby richardk » Sun May 10, 2009 2:26 pm

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^url/http:/(.+)$ /redirect.php?url=http://$1 [QSA,L]

# Ignore requests to existing files.
RewriteCond %{SCRIPT_FILENAME} !-f
# Ignore requests to existing directories.
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /get_link.php?pl=$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby dblanco » Sun May 10, 2009 4:41 pm

Perfect !!!

Well, I don't know how to thank you for all your help.

Thanks a lot again richardk.

With my best wishes from Spain :)

DaViD
dblanco
 
Posts: 10
Joined: Wed Apr 08, 2009 1:08 am

Postby dblanco » Sun Jul 12, 2009 7:09 pm

Hi,

now I need add two exceptions at the rule:

Code: Select all
RewriteRule ^([^/]+)/?$ get_link.php?pl=$1 [NC,L]


when I use this:

http://www.mydomain/user/ and http://www.mydomain/login

then must redirect to:

http://www.mydomain/user.php and http://www.mydomain/login.php

I tried this, but not working:

Code: Select all
RewriteCond %{REQUEST_URI} user
RewriteRule ^user/?$ user.php [NC,L]

RewriteCond %{REQUEST_URI} login
RewriteRule ^login/?$ login.php [NC,L]

Thanks
dblanco
 
Posts: 10
Joined: Wed Apr 08, 2009 1:08 am

Postby richardk » Wed Jul 15, 2009 12:29 pm

Those RewriteConds are pointless.

You would put them before the other rules
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^(login|user)/?$ /$1.php [QSA,L]

RewriteRule ^url/http:/(.+)$ /redirect.php?url=http://$1 [QSA,L]

# Ignore requests to existing files.
RewriteCond %{SCRIPT_FILENAME} !-f
# Ignore requests to existing directories.
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /get_link.php?pl=$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby dblanco » Thu Jul 16, 2009 7:55 am

chapeau !!!

understood. RewriteCond %{SCRIPT_FILENAME} mut go after the specific RewriteRule conditions.

Thanks a lot, again ;)
dblanco
 
Posts: 10
Joined: Wed Apr 08, 2009 1:08 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 100 guests

cron