Replace query string with file extension using htaccess - apache

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]

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]

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]

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.

Remove part of the query string with mod_rewrite

I am not very good with .htaccess at all, so I want to achieve something very simple, but I can't. What I want to do is to redirect certain files to test.php, and if test is ok, PHP redirects back to original page. It works fine, I add the "test=ok" part to the original URL, that way I don't get a redirect loop. However, I want to remove the test=ok query part from the original URL on redirection. How can I achieve that???
TL/DR
I have several URLs I want rewritten through mod_rewrite.
examples:
http://example.com/?time=1&test=ok
http://example.com/?test=ok
How can I remove the &test=ok and the ?test=ok parts using .htaccess?
Right now I have:
RewriteCond %{QUERY_STRING} ^test=ok$ [NC]
RewriteRule (.*) /$1? [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} (/[^.]*|\.(php|html?|js))$ [NC]
RewriteCond %{QUERY_STRING} !test=ok [NC]
RewriteRule .* test.php [L]
But that doesn't remove the test=ok part... :(

how do i get mod_rewrite to execute the RewriteRules if and only if there is no parameter q= in the url?

the below code works to deal with my url structures but i need the rules not to work if there is parameter q= in the url.
i tried to set up a rewritecond (in the commented out line below) without success,
please help! thanks :)
Options +FollowSymlinks
RewriteEngine on
# RewriteCond %{QUERY_STRING} q!=(.*)
RewriteRule ^FSD/([^/]+)/([^/]+)/ /index.php?service=$1&type=FSD [NC]
RewriteRule ^ECD/([^/]+)/([^/]+)/ /index.php?service=$1&type=ECD [NC]
RewriteRule ^([^/]+)/([^/]+)/ /index.php?category=$1&subcategory=$2 [NC]
RewriteRule ^([^/]+)/ /index.php?category=$1 [NC]
Your RewriteCond's test pattern is a little off, but you got the right idea:
RewriteCond %{QUERY_STRING} !(\A|&)q=.*
Note that the RewriteCond only applies to the next RewriteRule, so if you want to perform this exclusion for all four of your rules, you'll need to copy the RewriteCond above each of them.