简体   繁体   中英

I am having issues adding multiple url parameters to the same line in .htaccess

Currently my .htaccess looks like this


RewriteCond %{QUERY_STRING} ^&
RewriteRule ^$ - [R=404]

RewriteCond %{QUERY_STRING} ^age
RewriteRule ^$ - [R=404]

RewriteCond %{QUERY_STRING} ^gender
RewriteRule ^$ - [R=404]

RewriteCond %{QUERY_STRING} ^languages
RewriteRule ^$ - [R=404]

RewriteCond %{QUERY_STRING} ^sites
RewriteRule ^$ - [R=404]

RewriteCond %{QUERY_STRING} ^sortOrder
RewriteRule ^$ - [R=404]

RewriteCond %{QUERY_STRING} ^status
RewriteRule ^$ - [R=404]

RewriteCond %{QUERY_STRING} ^tags
RewriteRule ^$ - [R=404]

At the moment this works well, if I visit a URL with one of the parameters it will give me a 404 page, I want to know if there is a better way to write this.

Is it possible to combine all these into one line?

I have tried writing it like this

RewriteCond %{QUERY_STRING} ^&
RewriteCond %{QUERY_STRING} ^age
RewriteCond %{QUERY_STRING} ^gender
RewriteCond %{QUERY_STRING} ^languages
RewriteCond %{QUERY_STRING} ^sites
RewriteCond %{QUERY_STRING} ^sortOrder
RewriteCond %{QUERY_STRING} ^status
RewriteCond %{QUERY_STRING} ^tags
RewriteRule ^$ - [R=404]

But that didn't work as it would only work for the top query and not the rest of them

I have tried writing it like this

RewriteCond %{QUERY_STRING} ^& RewriteCond %{QUERY_STRING} ^age RewriteCond %{QUERY_STRING} ^gender RewriteCond %{QUERY_STRING} ^languages RewriteCond %{QUERY_STRING} ^sites RewriteCond %{QUERY_STRING} ^sortOrder RewriteCond %{QUERY_STRING} ^status RewriteCond %{QUERY_STRING} ^tags RewriteRule ^$ - [R=404]

RewriteCond directives ( conditions ) are implicitly AND'd, so the above will never be successful (ie. no 404 occurs) since the query string can not match all those strings at the same time.

You need to use the OR flag on all but the last condition . For example:

RewriteCond %{QUERY_STRING} ^& [OR]
RewriteCond %{QUERY_STRING} ^age [OR]
RewriteCond %{QUERY_STRING} ^gender [OR]
RewriteCond %{QUERY_STRING} ^languages [OR]
RewriteCond %{QUERY_STRING} ^sites [OR]
RewriteCond %{QUERY_STRING} ^sortOrder [OR]
RewriteCond %{QUERY_STRING} ^status [OR]
RewriteCond %{QUERY_STRING} ^tags
RewriteRule ^$ - [R=404]

However, this can be further reduced using regex alternation . For example, the above is the same as the following using just one condition :

RewriteCond %{QUERY_STRING} ^(&|age|gender|languages|sites|sortOrder|status|tags)
RewriteRule ^$ - [R=404]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM