Forum Moderators: phranque

Message Too Old, No Replies

?url=sth/a to /sth/a

         

blid

6:02 pm on Sep 19, 2010 (gmt 0)

10+ Year Member



Currently I can access specific site in two ways:

1. http://a.com/foo/bar
2. http://a.com/index.php?url=foo/bar

What I'm trying to achive is to allow to do it only using first way, and make redirect 301 on the second to the first one. Here's the code which I made so far and put into .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

#tricky part
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index.php\?url=?(.*)\ HTTP/ RewriteRule ^index.php$ http://a.com/$1 [R=301,L]


Currently it do almost all the job, it's redirecting index.php to /, but index.php?url=foo/bar to /?url=foo/bar and I can't manage to make it right.
TIA.

g1smd

7:12 pm on Sep 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Don't forget to escape literal periods in patterns.

You'll need %1 not $1 as the correct backreference.

Clear the appended query string by adding a trailing question mark to the end of the redirect target URL.

Change the pattern
^index.php$
to
^(index\.php)?$
so that requests for
http://example.com/?url=foo/bar
are also redirected.

Technically, the "/" is not allowed in the parameters attached to a URL, so you might yet encounter other issues.

blid

9:12 pm on Sep 19, 2010 (gmt 0)

10+ Year Member



@g1smd: Thank you for your answer.
I did the workaround in php, but I don't think it's "the right way" so I'm still struggling with it.
I followed your advice and now it looks like this:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?url=(.*)\ HTTP/
RewriteRule ^(index\.php)?$ http://example.com/%1? [R=301,L]


It doesn't work for ?url=foo/bar .
I'd also like to index.php (with no parameters) be redirected 301 to example.com, is it even possible without writing new set of rules ?

g1smd

10:12 pm on Sep 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As far as I know the "/" will have to be encoded for this to work.

The "/" is not allowed in parameters.

blid

10:56 pm on Sep 19, 2010 (gmt 0)

10+ Year Member



It's such a shame I can't edit my post, because obviously I forgot to mention that maybe ?url=foo/bar doesn't work but index.php?url=foo/bar works great.

jdMorgan

5:49 pm on Sep 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to make "index.php" optional in both lines...

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?url=(.*)\ HTTP/
RewriteRule ^(index\.php)?$ http://example.com/%1? [R=301,L]

Jim