views:

26

answers:

3

How can I change url in this way using htaccess:

http://example.com/page.php?go=something.php

should be redirected to:

http://example.com/something

If the get parameter name is different than 'go' leave as it is...

A: 

something like this (google around for the correct syntax)

RewriteEngine On
RewriteBase /
RewriteRule ^page.php?go=login.php /login [L]
JohnSmith
Lekensteyn
google around for correct syntax and how to get around those problems.e.g. add $ after login.php to prevent login.phpjunk from triggering
JohnSmith
A: 
if(!empty($_GET['go']) && is_string($_GET['go']) && $_GET['go'] == 'login.php'){
   header('Location: /login');
   exit;
}

Remember: no output (cookies, html) before this.

Lekensteyn
Dude, he asked for `htaccess`. You could at least state - in words - that this is PHP, or that you feel this should be done with PHP or...
LeguRi
Sorry, I skipped over that .htaccess part. In my opinion, this should be done with PHP, read my comment on JohnSmiths answer.
Lekensteyn
A: 

This should do:

# Assuming that "RewriteEngine On" has already been called
RewriteCond %{QUERY_STRING} ^(.*&)?go=([a-z]+)\.php(&.*)?$
RewriteRule page.php %2?

What happens here? First, RewriteCond matches a query string that contains the go=something.php, where "something" is captured by ([a-z]+). Then the RewriteRule uses the second capture group's contents from RewriteCond, containing "something.php". The question mark at the end gets rid of the original query string.

Note: if you want to preserve the rest of the query string excluding go=... parameter, things get a bit more complicated.

See the docs in http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html for more info.

Eemeli Kantola