RedirectMatch 301 remove query string / URL Parameters - apache

i need a Solution for the google search console error links and RedirectMatch 301 remove query string / URL Parameters in my htaccess:
Google Search Console Links:
https://www.domain.de/?main_page=index&zenid=umf5etlrmr4blnuiq0e4jsp6l2&cPath=15_326&sort=20a&alpha_filter_id=68
https://www.domain.de/index.php?main_page=product_reviews_write&products_id=9985&cPath=5_380&number_of_uploads=0
https://www.domain.de/?main_page=index&cPath=46_47&sort=20a&alpha_filter_id=70
https://www.domain.de/?currency=USD&main_page=products_new&disp_order=7&page=141
https://www.domain.de/?main_page=index&zenid=mj6nsb9r53goiu6e13nb80tfq7&cPath=1_160&sort=20a&alpha_filter_id=70
https://www.domain.de/?currency=USD&main_page=index&cPath=3_137
https://www.domain.de/?main_page=index&cPath=46_76&sort=20a&alpha_filter_id=84
https://www.domain.de/?main_page=index&cPath=5_6&sort=20a&alpha_filter_id=85
https://www.domain.de/index.php?main_page=index
And many more.
I test this way in the htaccess, but it didn't work:
RedirectMatch 301 ^/?main_page*$ https://www.domain.de/
RedirectMatch 301 ^/?main_page\=$ https://www.domain.de/
RewriteEngine on
RewriteCond %{QUERY_STRING} ^main_page$ [NC]
RewriteRule ^/?main_page?$ https://www.domain.de/ [L,R=301]

You can use the following generic rule to remove query strings
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(main_page|currency) [NC]
RewriteCond %{REQUEST_URI} ^/$ [OR]
RewriteCond %{REQUEST_URI} ^/index\.php$ [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R=301]

Adding one more approach of htaccess rules here, written as per shown samples. Using Apache's THE_REQUEST variable here.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(?:index\.php)?/?(?:\?(?:main_page|currency))?\s [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R=301]
OR as an alternative use following code(using QSD flag here to remove query string):
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(?:index\.php)?/?(?:\?(?:main_page|currency))?\s [NC]
RewriteRule ^ %{REQUEST_URI} [L,R=301,QSD]

Related

.htaccess rewriteCond part of the url

URL applying rule(s):
https://olddomain.ch/de/qahmkitd73/
https://olddomain.ch/fr/qahmkitd73/
https://olddomain.ch/en/qahmkitd73/
RewriteCond %{HTTP_HOST} chezcamillebloch\.ch$
RewriteCond %{REQUEST_URI} ^\/.*\/qahmkitd73\/$ [NC]
RewriteRule .* https://newdomain.ch/$1/qahmkitd73 [NE,R=301,L]
this should redirect to https://newdomain.ch/**/qahmkitd73
the ** should be dynamic from the URL (de/ or en/ or fr/)
You could keep your .htaccess rules file in following manner. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.ch$ [NC]
RewriteRule ^ http://newdomain.ch/%{REQUEST_URI} [R=301,NE,L]
this should redirect to https://newdomain.ch/**/qahmkitd73 the ** should be dynamic from the URL (de/ or en/ or fr/)
You may use this redirect rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.ch$ [NC]
RewriteRule ^(en|fr|de)/qahmkitd73/?$ http://newdomain.ch%{REQUEST_URI} [R=301,NC,L]

htaccess: rewrite and redirect 301

I rewrited, by .htaccess, a category of dynamic url generated by a query string in this mode:
RewriteEngine On
RewriteRule ^id1-([^-]*)-id2-([^-]*)$ /page.php?id1=$1&id=$2 [L]
Now my rewrite works in right way and, for example, the following urls drive to the same page:
http://www.mysite.it/id1-01234-id2-56789
http://www.mysite.it/page.php?id1=01234&id2=56789
But now I want a redirect 301, from second type to first type, for all dynamic urls. For example:
from
http://www.mysite.it/page.php?id1=01234&id2=56789
to
http://www.mysite.it/id1-01234-id2-56789
The following way doesn't work:
RewriteEngine On
RewriteRule ^id1-([^-]*)-id2-([^-]*)$ /page.php?id1=$1&id=$2 [L]
RewriteCond %{QUERY_STRING} (^|&)id1=$1($|&)
RewriteCond %{QUERY_STRING} (^|&)id2=$2($|&)
RewriteRule ^page\.php$ /id1-id2? [L,R=301]
Where is the error?
can you help me please?
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/page.php
RewriteCond %{THE_REQUEST} \?id1=(\w+)&id2=(\w+)\s
RewriteRule ^page.php /id1-%1-id2-%2? [NC,R=301,L]
RewriteRule ^id1-([^-]*)-id2-([^-]*)$ /page.php?id1=$1&id2=$2 [L]

Changing Redirect Match to Rewritw rule

For one of my website I have to change the redirect match to rewrite rule to keep the modules in sync and alternatively remove the mod_alias .
I tried few but could not succeed can someone please help here.
RewriteCond %{HTTP_HOST} ^(.*)mywebsite.com [NC]
RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ http://www.mywebsite.com/$1/$2/$4
RedirectMatch 301 ^/search/page/(.*)$ http://www.mywebsite.com/?s&paged=$1
Tried to use the mod_rewrite but no success
RewriteCond %{HTTP_HOST} ^(.*)mywebsite.com [NC]
RewriteRule ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ http://www.mywebsite.com/$1/$2/$4 [NC,L,R=301]
RewriteRule ^/search/page/(.*)$ http://www.mywebsite.com/?s&paged=$1 [NC,L,R=301]
You can use these 2 rules:
RewriteCond %{HTTP_HOST} ^(www\.)?mywebsite\.com$ [NC]
RewriteRule ^(\d{4})/(\d{2})/(\d{2})/(.*)$ /$1/$2/$4 [NC,L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?mywebsite\.com$ [NC]
RewriteRule ^search/page/(.+)$ /?s&paged=$1 [NC,L,R=301]

Create RewriteRule Split query string in .htaccess for _escaped_fragment_

I am redirecting requests to snapshots directory for google hashbang.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
RewriteRule ^(.*)$ /snapshots/%1? [NC,L]
So: http://site/?_escaped_fragment_=/detail/10
Go here: site/snapshots/detail/10
But I want to ignore the second params so
http://site/?_escaped_fragment_=/detail/10/12-12-2014
Should be redirect to the same url
site/snapshots/detail/10
Any idea how I should rewrite the rules?
Try changing this line:
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
to
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?([^/]+(?:/[^/]+|))
So the regex will only attempt to capture as many as 2 virtual paths.

Simple Apache mod_rewrite remapping

I have to remap a few ID's to URL strings (301 redirect) and I have to do it with mod_rewrite:
/page.php?id=15 to /pagexy
/page.php?id=10 to /pageyz
The rule:
RewriteRule ^page.php?id=15$ /pagexy [L,R=301]
doesn't work. What am I doing wrong?
You need to inspect the query string separately. The following should work:
RewriteEngine On
RewriteCond %{QUERY_STRING} id=15\b [NC]
RewriteRule ^/page.php$ /pagexy? [L,R=301]
RewriteCond %{QUERY_STRING} id=10\b [NC]
RewriteRule ^/page.php$ /pageyz? [L,R=301]
Given you are tying ids to pages, it may also pay to look at using a RewriteMap