rewrite query string with .htaccess without altering link structure

Viewed 42

I would like to remove the query string from the URL and rewrite it without the query string without having to re-code the link structure.

I have added this:

RewriteCond %{QUERY_STRING} .
RewriteRule ^$ /? [R,L]

to the end of my existing .htaccss code (so that it is the last thing that is done, since there are other re-write rules before this) but the query string is not removed. What am I doing wrong?

This is the content of my current .htaccess file:

RewriteEngine On

RewriteRule ^userfiles/ - [L]
RewriteRule ^userdata/ - [L]

RewriteCond %{THE_REQUEST} /index\.php\?p=(?!admin)(?!superadmin)((?![^&]*?edit)[^\s&]+) [NC]
RewriteCond %{THE_REQUEST} /index\.php\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,QSA]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]

Also I have a general question about how that affects the availability of query strings as variables... in other words if I re-write the URL to NOT show the query strings, is that query string still available if in PHP I write $_GET['q'] or will it then be not set anymore? Thanks

1 Answers

With your shown samples, could you please try following once. Please make sure to clear your browser cache before testing your URLs. While redirecting remove query string because your other rule is NOT actually redirecting, if this is what you want to have then this change may help you here.

RewriteEngine On
RewriteRule ^userfiles/ - [L]

RewriteRule ^userdata/ - [L]

RewriteCond %{THE_REQUEST} /index\.php\?p=(?!admin)(?!superadmin)((?![^&]*?edit)[^\s&]+) [NC]
RewriteCond %{THE_REQUEST} /index\.php\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]
Related