Redirection with parameters and multiple query - apache

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.

Related

Rewrite URL if specific parameter exists

I have a url like this: website.co.uk/search.php?a=category&b=test&c=test
I want to rewrite search.php to /search/ and the variable 'a' only if it exists.
So that the url would redirect to: website.co.uk/search/category/?b=test&c=test
If the url was: website.co.uk/search.php?b=test&c=test
Then this should redirect to website.co.uk/search/?b=test&c=test
Currently I am using:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^search/(.*?)/?$ search.php?a=$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /search\.php\?a=([^\&\ ]+)
RewriteRule ^/?search\.php$ /search/%1? [L,R=301]
If I enter the url website.co.uk/search.php?a=category&b=test&c=test
It redirects to website.co.uk/search/category
How do I add the remaining parameters to the end of this URL?
So that it looks like website.co.uk/search/category/?b=test&c=test
You can use these rules in your site root .htaccess:
Options -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} /search\.php\?a=([^&\s]*)\sHTTP [NC]
RewriteRule ^ /search/%1/? [L,NE,R=301]
RewriteCond %{THE_REQUEST} /search\.php(?:\?a=([^&\s]*)&(\S+))?\sHTTP [NC]
RewriteRule ^ /search/%1/?%2 [L,NE,R=301]
RewriteCond %{THE_REQUEST} /search\.php(?:\?(\S+))?\sHTTP [NC]
RewriteRule ^ /search/?%1 [L,NE,R=301]
RewriteRule ^search/?$ search.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^search/([\w-]+)/?$ search.php?a=$1 [L,NC,QSA]

Force HTTPS on all pages except one

I have a WordPress site that I want fully secured using SSL, with the exception of one page (it has a third party script who will not serve over SSL - let's say it's called /booking/), I also want all URL's redirected to the www version.
I've seen similar answers for the other way around, but not this specific use case.
Is this achievable using .htaccess?
Edit:
Here is my .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# BEGIN Custom
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^00\.00\.00\.00
RewriteRule (.*) https://www.mysite.co.uk/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} off
RewriteRule ^((?!online-booking).*) https://www.mysite.co.uk/$1 [NC,L,R,NE]
</IfModule>
# END Custom
*real IP replaced with zeros, this is to redirect the server IP to the actual website.
This code is for the page being at mysite.co.uk/online-booking/
Have it this way:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^00\.00\.00\.00
RewriteRule ^ https://www.mysite.co.uk%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /online-booking [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ http://www.%1%{REQUEST_URI} [L,R=302,NE]
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/online-booking [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=302,NE]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=302,NE]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
And make sure to test it after clearing your browser cache.
This should work :
RewriteEngine on
#--Redirect non-www or http requests excluding "/booking" to https--#
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} off
RewriteRule ^((?!booking).*) https://www.example.com/$1 [NC,L,R,NE]

Different .htaccess rewrite rules depending on a $_GET param

Is there a way to make different rewrite rules in .htacess if a certain GET parameter was passed?
For example, if query string is:
htttp://domain.com/?debug=1, than .htaccess should look like:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
If there is no debug=1 in query string, than:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule !\.(js|ico|gif|jpg|png|css|html|swf|flv|xml)$ index.php?$1 [QSA,L]
You can use RewriteCond like this:
RewriteEngine on
# ?debug=1 is present
RewriteCond %{QUERY_STRING} (^|&)debug=1\b [NC]
RewriteRule (.*) app/webroot/$1 [L]
# ?debug=1 is not present
RewriteCond %{QUERY_STRING} !(^|&)debug=1\b [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule !\.(js|ico|gif|jpg|png|css|html|swf|flv|xml)$ index.php?$1 [QSA,L]
You want to match against the query string in a RewriteCond:
RewriteCond %{QUERY_STRING} ^debug=1$ [NC]
RewriteRule (.*) app/webroot/$1 [L]
For more information, see the docs or this example.

Why does this not work, htaccess, capture domain ext and use in cond

Why does this rewrite rule not work?
RewriteCond %{HTTP_HOST} domain.(se|dk)/shop/$ [NC]
RewriteRule ^ https://domain.$1 [R=302,L,NE]
I am attempting to catch all requests to domain.dk/shop/ and domain.se/shop/ and redirect them to domain.dk, or domain.se.
What seems to be the issue?
Entire htaccess
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# Redirect everything to HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} domain.(se|dk)$ [NC]
RewriteRule ^shop$ https://domain.%1/ [R=302,L,NE]
</IfModule>
The HTTP_HOST doesn't have any path information, it is only "domain.se". The path information must be in the rule's regex match:
RewriteCond %{HTTP_HOST} domain.(se|dk)$ [NC]
RewriteRule ^shop$ https://domain.%1/ [R=302,L,NE]
Also, you need to use the %1 backreference, which references a grouping in the condition. The $1 backreference is one from the rule's grouping (which doesn't have one).

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]