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

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]

Related

Redirection with parameters and multiple query

I want to redirect this url
https://www.mydomain.fr/mon-compte-membre/?ihcnewlevel=true&lid=7&urlr=https%3A%2F%2Fwww.mydomain.fr%2Fservices%2F
To `https://www.mydomain.fr/slug/
EDIT
This is the edited code (still not working)
<IfModule mod_rewrite.c>
RewriteCond %{THE_REQUEST} /mon-compte-membre/ [NC]
RewriteCond %{QUERY_STRING} (^|&)ihcnewlevel=true(&|$) [NC]
RewriteCond %{QUERY_STRING} (^|&)lid=7(&|$) [NC]
RewriteCond %{QUERY_STRING} (^|&)urlr=https%3A%2F%2Fwww\.mydomain\.fr%2Fservices%2F [NC]
RewriteRule ^ /slug/ [R=302,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
You must use ? in the target to strip off any previous query string
Apache won't decode % characters in query string so you will have to match them literally
You may use:
RewriteCond %{THE_REQUEST} /mon-compte-membre/ [NC]
RewriteCond %{QUERY_STRING} (^|&)ihcnewlevel=true(&|$) [NC]
RewriteCond %{QUERY_STRING} (^|&)lid=7(&|$) [NC]
RewriteCond %{QUERY_STRING} (^|&)urlr=https%3A%2F%2Fwww\.mydomain\.fr%2Fservices%2F [NC]
RewriteRule ^ /slug/? [R=302,L]
Once you're satisfied with the result, replace 302 with 301.

htaccess redirect rule for multiple URL params

I need to get a redirect using htaccess
From: http://example.com/go/zaym?source=`PARAM1`&keyword=`PARAM2` but if there are no URL params I need also to get redirect with empty params
To: another domain http://example2.com/sub_id=PARAM`&sub_id2=PARAM2
So I need to move value source to sub_id and value keyword to sub_id2
I'm trying so, but it's not working:
RewriteCond %{QUERY_STRING} ^source=([^&]+)&keyword=([^&]+)$
RewriteRule ^go/zaym http://example2.com/sub_id=%1&sub_id2=%2 [R,L]
EDIT(More generic way): With more generic way as per OP's comment one could try following, where no hard coding of keyword or source strings, then try following.
With only query string condition check:
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^[\w-]+=(.*)(?!=&)$ [NC]
RewriteRule ^(.*)$ http://example2.com%{REQUEST_URI}?sub_id=%1 [NE,L]
RewriteCond %{QUERY_STRING} ^[\w-]+=([^&]*)&[\w-]+=(.*)$ [NC]
RewriteRule ^(.*)$ http://example2.com%{REQUEST_URI}?sub_id=%1&sub_id2=%2 [NE,L]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} ^/go/zaym/?$ [NC]
RewriteRule ^ http://example2.com%{REQUEST_URI} [L]
With URI + query string condition:
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^[\w-]+=(.*)(?!=&)$ [NC]
RewriteCond %{REQUEST_URI} ^/go/zaym/?$ [NC]
RewriteRule ^(.*)$ http://example2.com%{REQUEST_URI}?sub_id=%1 [NE,L]
RewriteCond %{REQUEST_URI} ^/go/zaym/?$ [NC]
RewriteCond %{QUERY_STRING} ^[\w-]+=([^&]*)&[\w-]+=(.*)$ [NC]
RewriteRule ^(.*)$ http://example2.com%{REQUEST_URI}?sub_id=%1&sub_id2=%2 [NE,L]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} ^/go/zaym/?$ [NC]
RewriteRule ^ http://example2.com%{REQUEST_URI} [L]

htaccess rewrite condition redirect to root domain

I am just simply trying to redirect several pages to the root domain and unsure why its failing, nothing happens.
RewriteEngine On
RewriteCond %{QUERY_STRING} /category/uncategorized/
RewriteCond %{QUERY_STRING} /2014/12/
RewriteCond %{QUERY_STRING} attachment_id=19
RewriteCond %{QUERY_STRING} attachment_id=18
RewriteCond %{QUERY_STRING} attachment_id=5
RewriteCond %{QUERY_STRING} attachment_id=20
RewriteCond %{QUERY_STRING} attachment_id=72
RewriteCond %{QUERY_STRING} attachment_id=6
RewriteCond %{QUERY_STRING} attachment_id=10
RewriteCond %{QUERY_STRING} attachment_id=15
RewriteCond %{QUERY_STRING} attachment_id=14
RewriteCond %{QUERY_STRING} attachment_id=16
RewriteCond %{QUERY_STRING} attachment_id=17
RewriteCond %{QUERY_STRING} attachment_id=99
RewriteCond %{QUERY_STRING} attachment_id=44
RewriteRule http://domain.com? [L,R=301]
The WP rewrites are in this same .htaccess but no change if I remove them.
Something like the below should work. Make sure it's in the .htaccess in your DocumentRoot
RewriteEngine On
RewriteRule category/uncategorized/? http://domain.com? [L,R=301]
RewriteRule 2014/12/? http://domain.com? [L,R=301]
RewriteCond %{QUERY_STRING} attachment_id=([56]|1[4-9]|20|44|72|99)
RewriteRule ^ http://domain.com? [L,R=301]
Edit: #arco444's answer will work for you (and is indeed a better answer), but it is essential that you try and understand exactly what is happening if you want to use it.
Option 1
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/category/uncategorized/ [OR]
RewriteCond %{REQUEST_URI} ^/2014/12/ [OR]
RewriteCond %{QUERY_STRING} attachment_id=19 [OR]
RewriteCond %{QUERY_STRING} attachment_id=18 [OR]
RewriteCond %{QUERY_STRING} attachment_id=5 [OR]
RewriteCond %{QUERY_STRING} attachment_id=20 [OR]
RewriteCond %{QUERY_STRING} attachment_id=72 [OR]
RewriteCond %{QUERY_STRING} attachment_id=6 [OR]
RewriteCond %{QUERY_STRING} attachment_id=10 [OR]
RewriteCond %{QUERY_STRING} attachment_id=15 [OR]
RewriteCond %{QUERY_STRING} attachment_id=14 [OR]
RewriteCond %{QUERY_STRING} attachment_id=16 [OR]
RewriteCond %{QUERY_STRING} attachment_id=17 [OR]
RewriteCond %{QUERY_STRING} attachment_id=99 [OR]
RewriteCond %{QUERY_STRING} attachment_id=44
RewriteRule ^ http://domain.com? [L,R=301]
The main thing here is the [OR] flag. In your original code, you were basically saying that all conditions had to be met. Remember that the last condition should not have the [OR].
Secondly, you were using QUERY_STRING to check for category/..., but that has nothing to do with the query string, which appear after the ?. So, I have change that to %{REQUEST_URI}.
Also, your RewriteRule did not have a URI to match against. I have added a ^ before the domain name to say that it should match everything (that is, provided the conditions have been met).
Option 2
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/category/uncategorized/ [OR]
RewriteCond %{REQUEST_URI} ^/2014/12/ [OR]
RewriteCond %{QUERY_STRING} attachment_id=(5|6|10|14|15|16|17|18|19|20|44|72|99)
RewriteRule ^ http://domain.com? [L,R=301]
Here, I have simplified the query string check. We now use one condition to check for different attachment IDs.
You could probably simplify this even further, but the purpose of my answer is to let you know where you went wrong, and where things can be simplified a little.

RewriteRule / RewriteCond help when two "keywords" show in the one query string

Currently I have this in my .htaccess file:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /forum/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(jpeg|jpg|gif|png)$ /forum/public/404.php [NC,L]
RewriteCond %{QUERY_STRING} (?:^|&)app=core(?:&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)section=register(?:&|$) [NC]
RewriteRule ^index\.php$ http://www.mysite.com/register.php? [NC,L,R=302]
RewriteCond %{QUERY_STRING} (?:^|&)app=core(?:&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)section=login(?:&|$) [NC]
RewriteRule ^index\.php$ http://www.mysite.com/login.php? [NC,L,R=302]
RewriteCond %{QUERY_STRING} (?:^|&)app=core(?:&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)section=lostpass(?:&|$) [NC]
RewriteRule ^index\.php$ http://www.mysite.com/retrieve_password.php? [NC,L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /forum/index.php [L]
</IfModule>
The one I need help with is this one:
RewriteCond %{QUERY_STRING} (?:^|&)app=core(?:&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)section=login(?:&|$) [NC]
RewriteRule ^index\.php$ http://www.mysite.com/login.php? [NC,L,R=302]
Currently this will redirect something like:
http://www.mysite.com/forum/index.php?app=core&module=global&section=login
The &module=global isn't required so as I'm sure you can tell it only looks for the app & section parameters.
However the problem is, it also redirects the logout link I want to redirect somewhere else as it also contains app=core & &section=login.
Here is an example:
http://www.mysite.com/forum/index.php?app=coree&module=global&section=login&do=logout&k=xxxxxxxxxxxxxxxxxxx
So basically the login should still redirect as above unless it has &do=logout in the query string; in that case I want to redirect it somewhere else.
How can I go about doing this?
Change your rule to:
RewriteCond %{QUERY_STRING} (?:^|&)app=core(?:&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)section=login(?:&|$) [NC]
RewriteCond %{QUERY_STRING} !(?:^|&)do=logout(?:&|$) [NC]
RewriteRule ^index\.php$ http://www.mysite.com/login.php? [NC,L,R=302]
Here RewriteCond %{QUERY_STRING} !(?:^|&)do=logout(?:&|$) [NC] will skip redirect if &do=logout is present in the query string.
EDIT: As per comments:
RewriteCond %{QUERY_STRING} (?:^|&)app=core(?:&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)section=login(?:&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)do=logout(?:&|$) [NC]
RewriteRule ^index\.php$ http://www.mysite.com/logout.php? [NC,L,R=302]

Adding some extra rewrite code to existing main rewrite code

I currently have this in my .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteRule ^(.*),(.*)$ $2.php?rewrite_params=$1&page_url=$2
RewriteCond %{QUERY_STRING} base64_encode.*(.*) [OR]
RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|[|%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|[|%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]
I want to add some new code that rewrites:
stores/some-name/
to:
shop.php?shop_id=some-name
How can I do this without affecting the other rewrites or the other rewrites affecting the addition?
You can add your new rule in the end:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteRule ^(.*),(.*)$ $2.php?rewrite_params=$1&page_url=$2 [L,QSA]
RewriteCond %{QUERY_STRING} base64_encode.*(.*) [OR]
RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|[|%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|[|%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]
RewriteRule ^stores/([0-9a-z.-]+)/?$ shop.php?shop_id=$1 [L,QSA,NC]