King Julien
King Julien

Reputation: 11308

htaccess rewrite rule, remove question mark

Let's say I have the following url:

http://example.com/?param

how should I remove the question mark from the URL, ie. rewrite

http://example.com/param

to something like this:

http://example.com/index.php?param

Here is my code, that doesn't work:

RewriteEngine On
RewriteRule ^(.*)$ /index.php?$1 [P]

Upvotes: 2

Views: 8296

Answers (1)

Jon Lin
Jon Lin

Reputation: 143876

Two completely different things need to happen. First you need to externally redirect the browser to show something different in the URL address bar. Second when the browser resends the 2nd request, the server internally rewrites the query string back. You can't arbitrarily add or remove things in URLs in the wild, as they are locators. You can create a new locator, tell the browser to use this new one instead of the old one, then internally on the server change the new one back to the old one.

See the top part of this answer for an explanation

To make the browser go to the new URL:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]

This takes a request for the URL: http://example.com/?something and redirects the browser to the URL: http://example.com/something

Then you need to internally rewrite it back:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]

When the request is made for http://example.com/something, the server rewrites the URI to /index.php?something. This is internal to the server so the browser knows nothing about it and will continue to display the URL http://example.com/something while the server processes the URI /index.php?something.

Upvotes: 7

Related Questions