.Htaccess redirect redirection

htaccess is on, canonization works for me (no-www to www.)

I am trying to use htaccess to do the following

www.domain.com/page.php?i=Page1 www.domain.com/page.php?i=Page2 

To

 www.domain.com/Page1 www.domain.com/Page2 

I tried using this piece of code until lucky:

 rewriterule ^([a-zA-Z0-9_-]+)/$ page.php?i=$1 

However, I think I'm going the other way around. I can not find an example for this. I am this one , but I cannot make it work.

0
source share
3 answers

Try the following:

 RewriteEngine On RewriteCond %{REQUEST_URI} ^/page\.php$ RewriteCond %{QUERY_STRING} ^i=([a-zA-Z0-9_-]+)$ RewriteRule ^(.*)$ http://www.domain.com/%1? [R=302,L] 

taken from: http://www.simonecarletti.com/blog/2009/01/apache-query-string-redirects/

+2
source

You want www.domain.com/Page1 appear in the address bar of the browser, but inside this URL should be /page.php?i=Page1 . In this case, the problem is completing / in your regex:

 Rewriterule ^([a-zA-Z0-9_-]+)/$ page.php?i=$1 ^---here 

Your desired URL /Page1 does not have a trailing slash, but rewrite requires one for the regex, so the pattern doesn't match and rewriting doesn't happen. Try removing / and see if this helps:

 Rewriterule ^([a-zA-Z0-9_-]+)$ page.php?i=$1 ^---no / 
0
source

To parse a query string, you need to access it in a RewriteCond

 # We need this line because /page.php?i=page.php will cause an infinite loop RewriteCond %{QUERY_STRING} !i=page.php # Now we parse out the value of i from the query string RewriteCond %{QUERY_STRING} i=([^&]+) RewriteRule ^page.php /%1 [L] 
0
source

All Articles