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.
Related
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]
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]
I have this link
https://career.guru99.com/top-50-oops-interview-questions/?format=pdf
I want to redirect it to
https://career.guru99.com/pdf/top-50-oops-interview-questions.pdf
I created the following htaccess rule
RewriteEngine On
RewriteCond %{QUERY_STRING} format=pdf [NC]
RewriteRule ^top-50-oops-interview-questions /pdf/top-50-oops-interview-questions.pdf? [R=301,L]
But the challenge is I have 200+ links and I will have to manually add so many entries in the htacess which also slow down the site. Is there some regular expression that can help with this?
I want /?format=pdf to be replaced with .pdf
Could you please try following, written and tested with shown samples. I am going through variable THE_REQUEST and getting appropriate format value from it then while rewriting placing its value.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s(.*?)/\?format=([^\s]*)\s [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/%2%1.%2 [NE,QSD,L]
I want /?format=pdf to be replaced with .pdf:
You may try this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^format=(pdf)$ [NC]
RewriteRule ^([\w-]+)/?$ /%1/$1.%1? [R=301,END,NE]
I tried to change the browser url from localhost:8888/dev/test.php to localhost:8888/dev/testRewrite/ following the example of this question on S.O.
At first, I tried the provided code in that question
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^testRewrite/$ test.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /test\.php [NC]
RewriteRule ^test.php$ /testRewrite/ [L,R=301]
and managed to change localhost:8888/test.php to localhost/testRewrite/
But applying the same to the sub-directory /dev/ resulted in no change.
I tried adding RewriteBase /dev/, moving the .htaccess to the sub-directory and even clearing the cache but it made no difference and the url remained http://localhost:8888/dev/test.php. So I think I have not understood the rewrite rule properly and am doing it the wrong way.
So what is the right way of doing this? Also does the .htaccess need to be in the sub-directory or it can be in the root directory?
After a little bit more tinkering and searching, I finally got what I needed
RewriteEngine on
RewriteCond %{THE_REQUEST} (\S*?)/test\.php [NC]
RewriteRule ^ %1/test/ [L,R=301]
RewriteRule ^(.*)/test/ /$1/test.php [NC,L]
Regex substitution was the solution to my requirements
Considering your code
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^testRewrite/$ test.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /test\.php [NC]
RewriteRule ^test.php$ /testRewrite/ [L,R=301]
The problem lies in the last line.
^test.php$
In Regex ^ symbolize the start of text and $ the end of text.
Thus ^test.php$ has only one exact match. "test.php" But what you to redirect is "/dev/test.php".
Thus your last line needs to be:
RewriteRule test.php$ /testRewrite/ [L,R=301]
If you want to be very exact, you could also make it
RewriteRule ^/dev/test.php$ /dev/testRewrite/ [L,R=301]
You might need to do the same in line 3.
I have links to user profiles like:
https://example.com/chat/index.php?action=user&member=547
https://example.com/chat/index.php?action=user&member=11540
etc.
My goal is to mod-rewrite such URLs so that they look nicer, ie. they should look like:
https://example.com/chat/member-547/
https://example.com/chat/member-11540/
etc.
And URLs without a trailing slash, ie.
https://example.com/chat/member-547
should be 301-forwarded to one with slash, ie.
https://example.com/chat/member-547/
So in .htaccess I tried this, but only the first line seems to work and it's not complete:
RewriteRule ^chat/member-([0-9]+)/$ ./index.php?action=user&member=$1
RewriteRule ^chat/member-([0-9]+)$ /member-$1/ [R=301,L]
TO SUMMARIZE:
When someone enters URL like:
https://example.com/chat/index.php?action=user&member=547
it should be 301-redirected to:
https://example.com/chat/member-547/
When someone enters:
https://example.com/chat/member-547
it should also be 301-redirected to:
https://example.com/chat/member-547/
I hope there's an efficient way to do it right.
Starkeen's answer is correct, and will work. I'll just suggest an alternative, which acts on the raw request variable itself:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /chat/index\.php\?action=user&(member)=(\d+) [NC]
RewriteRule ^chat/index\.php$ /chat/%1-%2/? [R=301,L]
RewriteRule ^chat/member-([0-9]+)$ /member-$1/ [R=301,L]
RewriteRule ^chat/(member)-(\d+)/$ /chat/index.php?action=user&$1=$2 [L]
You can use the following rule :
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^action=([^&]+)&member=(.+)$
RewriteRule /chat/index\.php$ /chat/member-%2/? [L,R=301]
RewriteRule ^chat/member-(.+)/?$ /chat/index.php?action=user&member=$1 [L]