Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

rewrite query string with .htaccess without altering link structure

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

question from:https://stackoverflow.com/questions/65866501/rewrite-query-string-with-htaccess-without-altering-link-structure

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...