Redirect urls with parameters to homepage url - regex

I have the following example urls that i need redirecting to www.example.co.uk
http://www.example.co.uk/wordpress/?feed=comments-rss2
http://www.example.co.uk/wordpress/?feed=rss2
http://www.example.co.uk/wordpress/?cat=518
http://www.example.co.uk/wordpress/?cat=514
http://www.example.co.uk/wordpress/?cat=520
http://www.example.co.uk/wordpress/?cat=521
http://www.example.co.uk/wordpress/?cat=523
http://www.example.co.uk/wordpress/?cat=515
http://www.example.co.uk/wordpress/?p=119
There are many more urls further to this list that all have a wordpress/ notation.
Would I have to write a conditional rewrite for each like this:
RewriteCond %{QUERY_STRING} ^feed=comments-rss2$
RewriteRule ^/wordpress/?(.*)$ http://www.example.co.uk [R=301,L]
RewriteCond %{QUERY_STRING} ^cat=518$
RewriteRule ^/wordpress/?(.*)$ http://www.example.co.uk [R=301,L]
or is there a rule i can write that redirect all urls that have /wordpress in them to redirect to www.example.co.uk

I believe a single rule like would be suffice:
RewriteCond %{QUERY_STRING} (^|&)(feed|p|cat)=[^&]+ [NC]
RewriteRule ^wordpress(/.*)?$ http://www.example.co.uk/? [R=301,L,NC]
Make sure this is very first rule in your .htaccess just below RewriteEngine On line.

Related

Handle one query parameter specifically

I need to handle one query parameter specifically.
The url can look like the following:
https://example.com/?app=egov&map=1337
https://example.com/?app=egov&map=1337&showoptions=true
https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorld
The redirect should point to
https://example.com/contextName/resources/apps/egov/index.html?map=1337
https://example.com/contextName/resources/apps/egov/index.html?map=1337&showoptions=true
https://example.com/contextName/resources/apps/egov/index.html?map=1337&anotherparam=helloWorld
As you can see, the app-Parameter needs to be used as an URL-part; the others need to be used like traditional query-params.
I've figured out, how to implement the first part, where https://example.com/?app=egov points to https://example.com/contextName/resources/apps/egov/index.html using the following
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^app\=(.+)
RewriteRule ^/$ "/contextName/resources/apps/%1/index.html" [R,L,QSA]
but I'm struggeling how to realize the correct handling of the other params.
All these combination of query string can be handled by a single rule loke this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^app=([\w-]+)(?:&(.*))?$ [NC]
RewriteRule ^$ contextName/resources/apps/%1/index.html?%2 [L]
RewriteCond matches app query parameter and captures it in %1 while rest of the query string after & is captured in %2.
With your shown samples, please try following htaccess Rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
####Apache documentation link https://httpd.apache.org/docs/trunk/rewrite/remapping.html for more info.
##Internal rewrite rule for URL https://example.com/?app=egov&map=1337
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2 [L]
##Internal rewrite rule for URL https://example.com/?app=egov&map=1337&showoptions=true
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)&showoptions=(.*)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2&showoptions=%3 [L]
##Internal rewrite rule for https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorl
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)&showoptions=([^&]*)&anotherparam=(.*)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2&anotherparam=%4 [L]
All of your URLs uri part is NULL, so rules are written based on that.

I'm having an issue with my .htaccess redirection

I need to use this url http://yoursite.com/proprietes.php?viewid&mls=[18348939] but I wan it to go on a pdf a-very-new-post.pdf. I want to do a redirection in the htaccess. It is not working... I'm in wordpress
thanks!
RewriteEngine On
Redirect 301 /proprietes.php?viewid&mls=[18348939]$ http://yoursite.com/a-very-new-post.pdf
Redirect or RewriteRule directives cannot match query string. Use a RewriteCond instead like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/proprietes\.php\?viewid&mls=\[?18348939\]? [NC]
RewriteRule ^ /a-very-new-post.pdf? [L,B,R=301]
RewriteCond %{THE_REQUEST} \s/en/proprietes\.php\?viewid&mls=\[?18348939\]? [NC]
RewriteRule ^ /a-very-new-post-english.pdf? [L,B,R=301]

Apache RewriteEngine

I want to rewrite an URL like this
http://domain1.com/some_directory/12345678?x=1/audio.mp3
to an URL like this http://domain2.com/12345678?x=1.
I tried this rewrite rule RewriteRule /some_directory/(.*)$/(.*).mp3 http://domain2.com/$1 [NC,L] but it doesn't work.
You can use this rule in root .htaccess by matching query string in a RewriteCond:
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)(x=\d+)[^.]+\.mp3 [NC]
RewriteRule ^some_directory/(.*)$ http://domain2.com/$1?%1 [NC,L,R=302,NE]

.htaccess rewrite rule with parameters

I have a lot of urls in the following format.
index.php?option=com_letflat&task=view&id=42
index.php?option=com_letflat&task=view&task=ajaxmap
I want to rewrite all urls that contain option=com_letflat to the root of the domain, ie http://www.example.com
I have tried dozens of different things, and none seem to work.
I have got as far as this:
RewriteCond %{QUERY_STRING} (^|&)option=com_letflat [NC]
RewriteRule ^index.php(.*) http://www.example.com [R=301,L]
But it doesn't work.
Thanks
You can use this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)option=com_letflat(&|$) [NC]
RewriteRule ^index\.php /? [R=301,L]
Just make sure this is very first rule in your .htaccess

301 Redirect to URL with query strings

I'm trying to redirect one URL to another URL. They are different domains, but both have query strings in their URLs. The sample URLs are:
Redirect:
olddomain/faculty/profile?uID=123
to:
newdomain/Faculty?id=024
This is what I've placed in the .htaccess file and it does not work (all modules necessary are installed and activated as there are other 301's without query strings that work without issue):
RewriteCond %{HTTP_HOST} ^www\.olddomain\.tld$ [NC]
RewriteCond %{QUERY_STRING} ^uID=123$ [NC]
RewriteRule ^faculty/profile$ http://newdomain/Faculty?id=024 [R=301,NE,NC,L]
What am I doing wrong?
You need to add http:// also:
RewriteCond %{HTTP_HOST} ^www\.olddomain\.tld$ [NC]
RewriteCond %{QUERY_STRING} ^uID=123$ [NC]
RewriteRule ^faculty/profile$ http://newdomain/Faculty?id=024 [R=301,NE,NC,L]
And make sure this rule is placed at top just below RewriteEngine On.