3

I have a rewrite rule for my site that passes everything after the domain name as a parameter:

RewriteRule ^([0-9a-zA-Z_=/&\-]+)?$ index.php?page=$1   [L,QSA]

But when going to:

mydomain.co.uk/mypage

..it is appending the query string after the URL (In the browser address bar) like this:

mydomain.co.uk/mypage?page=mypage

I thought it was just Chrome being weird... but it does it in IE aswell.

Has anyone ever had this? Any help would be great I just need it not to append the query string to the end and stay as "mydomain.co.uk/mypage"

Zephni
  • 753
  • 1
  • 7
  • 26
  • You're using codeigniter right? Because I don't think that rewrite rule does what you think it does. – evolutionxbox Jun 15 '15 at 14:39
  • Nope, this is my own thing. Everything works perfectly, and doesn't break because of this problem. I just would like it to not push the query string to the URL so I can keep the URL pretty – Zephni Jun 15 '15 at 14:41
  • My understainding is that the rewrite rule takes the URL `mysite.com/my_wonderful_page` and passes it to the server as `mysite.com/index.php?page=my_wonderful_page`. Right? – evolutionxbox Jun 15 '15 at 14:45
  • Yep, that is working I can get the page passed with $_GET["page"].. but for some reason in my case it's actually changing the URL in the browser address bar by putting ?page=something at the end – Zephni Jun 15 '15 at 14:47
  • This is similar to what Codeigniter does. Here's something you could try based on their solution: ```RewriteEngine on RewriteCond $1 !^(index\.php|images|robots\.txt) RewriteRule ^(.*)$ /index.php?page=$1 [L]``` – evolutionxbox Jun 15 '15 at 14:49
  • I've tried that but unfortunately get the same issue :( – Zephni Jun 15 '15 at 16:06
  • Make sure you have `RewriteEngine on` otherwise it won't work. Also, take a look at http://stackoverflow.com/questions/8936716/htaccess-url-rewriting-challenge?rq=1 – evolutionxbox Jun 15 '15 at 16:09
  • 2
    What are you other .htaccess rules? That 1 line can't be the only thing in your .htaccess. – Panama Jack Jun 15 '15 at 17:54

1 Answers1

0

You could use a condition to check if the requested file exists.

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

I tested this code with htaccess.mwl.be for this requested url http://mydomain.co.uk/mypage?page=mypage and it redirects to http://mydomain.co.uk/index.php?page=mypage

Phil
  • 422
  • 1
  • 5
  • 20