Redirect stripping the matched string - Htaccess - apache

Using .htaccess, I want to redirect all the pages which contains --nocolor- at the end of their url. I want redirect them to the same url without the match.
Example:
https://www.test.com/products/test--nocolor-
to
https://www.test.com/products/test
What I've tried is
RewriteCond %{REQUEST_URI} (--nocolor-)
and that is working but I cannot get the right url while rewriting.
I've tried
RewriteRule ^products/([^/]*)(?=--nocolor-) /$1 [R=301,L]
RewriteRule ^(.+)(?=--nocolor-) /$1 [R=301,L]
RewriteRule ^.*(?=--nocolor-) /$1 [R=301,L]
but I always end up with $1 empty.
What do I do wrong?

With your shown samples, attempts please try following .htaccess rules. Make sure to place these rules at top of your .htaccess rules.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(.*)--nocolor-\s [NC]
RewriteRule ^ /%1 [R=301,L]

Related

301 Redirect in htaccess for URLs having Parameter P

I need to set 301 redirect in htaccess for URLs having parameter P. One example URL is
http://www.price4india.co.in/vivo-x20-plus-ud-price-in-india-scanner-feature-real.html?p=1028
to
http://www.price4india.co.in/vivo-x20-plus-ud-price-in-india-scanner-feature-real.html
After redirect everything after .html shall get removed and the value after P=...... can be any numerical value. So far I have tried below query but it is not working. Any suggestion please...
RewriteCond %{QUERY_STRING} ^p(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
With your shown samples, please try following .htaccess rules file. Make sure to keep your .html file and .htaccess files in root path only.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(.*\.html)\?p=\d+\s [NC]
RewriteRule ^ /%1? [R=301,L]
NOTE: In case you have further more rules in your .htaccess rules, which includes internal rewrite of html files then you could keep these rules above those.
RewriteCond %{QUERY_STRING} ^p(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
This is almost correct, except the regex ^p(&|$) is incorrect. This matches p&<anything> or p exactly. Whereas you need to match p=<anything> (eg. ^p=) or p=<number> (eg. ^p=\d+). This is of course assuming the p URL parameter always occurs at the start of the URL-path (as in your example).
For example:
RewriteCond %{QUERY_STRING} ^p= [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]

apache Rewrite rule appends /html directory for no reason

I have a domain foo.tech.
I want to use a new domain footech.io instead.
The redirect also has to make sure all the URLs work.
E.g foo.tech/bar goes to footech.io/bar
Here is my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^(.*) http://footech.io/$1 [R=301,L]
For some reason, it decides to add /html at the end of my domain.
So now if I visit foo.tech it will redirect to footech.io/html
If I visit foo.tech/bar it will redirect to footech.io/html/bar
Please help.
Update:
I think the /html comes from the $1
I've tried to make the rewrite rule as follows:
RewriteRule ^(.*) http://footech.io/$1/$1 [R=301,L]
going to foo.tech leads to footech.io/html//html/
going to foo.tech/bar leads to footech.io/html/bar/html/bar
final update:
I made it work now using this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^html/(.*) http://footech.io/$1 [R=301,L]
This seems to fix it
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^html/(.*) http://footech.io/$1 [R=301,L]

htaccess redirect urls with parameters to specific directory

i want to redirect specific urls from
http://www.domain.com/com/page.asp?id=99
to
https://www.domain.com/de/directory/
and all urls from
http://www.domain.com/com/page.asp
to
https://www.domain.com/de/
ive tried some redirects but i cant get it done right.
Please help me!
RewriteCond %{query_string} id=5
RewriteRule (.*) https://www.domain.com/de/directory/? [R=301,L]
this works, but the biggest problem is all urls in the cms https://www.domain.com/cms are redirected too if they contain an id
Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=99$
RewriteRule ^page\.asp$ /de/directory/ [L,R]
RewriteRule ^page\.asp$ /de/ [L,R]

htaccess redirecting one query string to another

I'd like to redirect http://domain.com/videos/?src=category1&video=video1
To http://domain.com/videos/watch?v=1
The obvious, just using a Redirect 301 /old /new doesn't work here. Here's my starting point and I can't seem to get this to redirect properly. This .htaccess file lives in the videos directory.
RewriteCond %{QUERY_STRING} ^src=category1&video=video1$ [NC]
RewriteRule /videos/watch\?v=1 [L,R=301]
Try:
RewriteCond %{QUERY_STRING} ^src=category1&video=video1$ [NC]
RewriteRule ^$ /videos/watch?v=1 [L,R=301]
You were missing the rewrite rule's regex pattern.

Htaccess ReWrite URL with ? in duplicate URL's

We have moved from IIS to Apache and used the below rewrite rule to redirect www.example.co.uk/Default.aspx?pagename=About-Us to the new page url www.example.co.uk/About-Us/
RewriteCond %{REQUEST_URI} ^/Default\.aspx$
RewriteCond %{QUERY_STRING} ^pagename=About-Us$
RewriteRule ^(.*)$ http://www.domain.co.uk/About-Us/ [R=301,L]
The problem is this rule is showing up as duplicate content in google WMT and the page is being served at both URL's...
Can anyone spot what is wrong with this rule?
Change your rule to this:
RewriteCond %{QUERY_STRING} ^pagename=About-Us$ [NC]
RewriteRule ^Default\.aspx$ http://www.domain.co.uk/About-Us/? [R=301,L,NC]
Also note that it will take some time for search bots to remove duplicate URIs.