301 Redirect in htaccess for URLs having Parameter P - apache

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]

Related

Htaccess taugh redirection rule

I have to create such a redirection rule in HTACCESS that changes the first part of the URL (from "confindustria/verona/news.nsf/($linkacross)" to "new.bestrank.it/gate/news/documento?openform&id="), keeps unchanged the ID ("879D9288439B4C42C12588E1004BD915&restricttocategory=Credito%20Finanza%20Assicurazioni") but extracts a static piece of it in the middle ("?opendocument").
From
https://old.bestrank.it/confindustria/verona/news.nsf/($linkacross)/879D9288439B4C42C12588E1004BD915?opendocument&restricttocategory=Credito%20Finanza%20Assicurazioni
to:
https://new.bestrank.it/gate/news/documento?openform&id=879D9288439B4C42C12588E1004BD915&restricttocategory=Credito%20Finanza%20Assicurazioni
How should I write the rules for this?
The best I came up with is the following:
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/confindustria/verona/news.nsf/($linkacross)/( 879D9288439B4C42C12588E1004BD915)?opendocument(&restricttocategory=Credito%20Finanza%20Assicurazioni)/?\s [NC]
RewriteRule ^ https://new.bestrank.it/gate/news/documento?openform&id=%1%2? [R=301,NC,L]
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
Either use following Rules OR use rules of OR here, use them one at a time only please.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/old-path/(id-3434)-newfolder(-5455)/?\s [NC]
RewriteRule ^ https://new-domain.com/new-path/%1%2? [R=301,NC,L]
OR as a Generic rules(not hardcoding digits after id) try following:
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/old-path/(id-\d+)-newfolder(-\d+)/?\s [NC]
RewriteRule ^ https://new-domain.com/new-path/%1%2? [R=301,NC,L]

Redirect stripping the matched string - Htaccess

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]

Can I use HTACCESS to format this URL?

I would like to know if it is possible to format a URL like https://site1.com/en/article-details.test-article?item=test to https://site1.com/en/article-details/test-article?item=test so basically changing the .test-article to /test-article.
Because of how my site works it only recognizes .test-article as a valid selector so reading up https://httpd.apache.org/docs/2.4/rewrite/flags.html it seems I may need to use a PT flag to show the user the desired URL but pass the correct one to my site.
So far I have this to replace the dot for a slash
RewriteCond %{REQUEST_URI} ^/([a-z]{2})/article-details.([a-z]{2})(/?)$
RewriteRule ^/([a-z]{2})/.* https://%{SERVER_NAME}/$1/$2 [L,NC,R=301]
Edit(this works to format the URL as per RavinderSingh13's suggestion)
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/([a-z]{2})/article-details.([^?]*)(/?)$
RewriteRule ^ https://%{SERVER_NAME}/%1/article-details/%2 [QSA,R=301,NE,L]
With your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(en/article-details)\.(test-article)\?item=test\s [NC]
RewriteRule ^ https://site1.com/%1/%2 [QSA,R=301,NE,L]
As per OP's comments adding more Generic rules here:
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(en/article-details)\.([^?]*)\?item=test\s [NC]
RewriteRule ^ https://site1.com/%1/%2 [QSA,R=301,NE,L]
You may try this rule as your topmost rule:
RewriteEngine On
RewriteRule ^(.+)\.(test-article/?)$ /$1/$2 [R=302,NE,L,NC]
Change 302 to 301 after testing this rule.

Rewrite condition to particular URL and redirect

I have a redirect rule that redirects a url of pattern /abcd/* to http://myweb.com/abcd/*.
I want to apply this redirect except /abcd/index.html.
I have tried this.
RewriteCond %{THE_REQUEST} !^/abcd/index
RewriteCond %{REQUEST_URI} !^/abcd/index [NC]
RewriteRule ^/abcd/(.+)$ http://myweb.com/abcd/$1 [NC,R=301,L]
I am not sure if it is correct.
Please suggest me the correct way of doing this.
Try this rule without leading slash in RewriteRule:
RewriteCond %{REQUEST_URI} !^/abcd/index [NC]
RewriteRule ^abcd/(.+)$ http://myweb.com/abcd/$1 [NC,R=301,L]
Difference is ^abcd/(.+)$ instead of ^/abcd/(.+)$
Your 2 conditions were redundant so I reduced it to one.
.htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern.

htaccess - rewritecond issue

I have an htacess file for apache that isn't working properly.
I have a default cactch-all that grabs all requests however it's still catching the matched URL above it (ajax/*). I thought that condition to match urls not containing ajax would stop it but it isn't.
RewriteRule ^ajax/(.*)$ process_lite.php [QSA,L]
RewriteRule ^resources/(.*)$ resources/$1 [L]
RewriteCond %{REQUEST_URI} !\.(css|jpg|js|gif|png)$
RewriteCond %{REQUEST_URI} !^ajax
RewriteRule ^(.*)$ process.php [QSA,L]
Can someone help me out?
It's probably because the first rule matches /ajax/ requests and rewrites it to /process_lite.php, thus the RewriteCond %{REQUEST_URI} !^ajax doesn't match since the URI is now process_lite.php. The regular expression you use won't match anything anyways because REQUEST_URI variable will start with a leading slash. You can try changing the condition to:
RewriteCond %{REQUEST_URI} !^/process_lite.php
Additionally, the "resources" rule doesn't seem to do anything except end rewriting, you could change it to this if that's the goal:
RewriteRule ^resources/ - [L]