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

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]

Related

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]

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]

Add HTTPS to already existing Mod Rewrite

I have the following rules and conditions in my mod_rewrite and like to add HTTPS to it. I like to confirm the following it accurate before publishing it live.
Current Rules and Conditions
RewriteEngine On
RewriteCond %{REQUEST_URI} \.jpg$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /home/domain.com/public_html/missing.png [L]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^index.html$ http://www.domain.com/ [L,R=301]
New Rules and Conditions
RewriteEngine On
RewriteCond %{REQUEST_URI} \.jpg$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /home/domain.com/public_html/missing.png [L]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTPS} Off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^index.html$ https://www.domain.com/ [L,R=301]
RewriteEngine On
# always do domain/https redirs first
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{HTTP_HOST} ^(?!www\.domain\.com$). [NC]
RewriteCond %{THE_REQUEST} ^\S+\s+/?(\S*)
RewriteRule ^ https://www.domain.com/%1 [NS,NE,L,R=301]
# remove index files from URLs, except from POST forms, etc. you can add more file extensions to the second condition
RewriteCond %{REQUEST_METHOD} ^(?:GET|HEAD)$
RewriteCond %{THE_REQUEST} ^\S+\s+/?((?:[^?/]*/)*?)index\.(?:html?|php|pl)(\?\S*)?(?:\s|$)
RewriteRule ^ https://www.domain.com/%1%2 [NS,NE,L,R=301]
# missing images
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(?:jpe?g|png|gif)$ /missing.png [NS,NC,L,DPI]

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]