I have this link https://career.guru99.com/top-50-c-sharp-interview-questions-answers/?format=pdf
I want to redirect it to https://www.guru99.com/pdf/c-sharp-interview-questions.pdf
I created the following htaccess rule
RewriteCond %{QUERY_STRING} format=pdf [NC]
RewriteRule ^c-sharp-interview-questions.html /pdf/c-sharp-interview-questions.pdf? [R=301,L]
But the challenge is I have 100+ 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
2
Answers
1st Solution: Try following in case you are hitting
http://localhost:80/top-50-c-sharp-interview-questions-answers/?format=pdf
in your browser. Change[NC,L]
TO[R=301,NC,L]
in case you want to redirect your URL in browser.2nd solution: Could you please try following, written based on your shown samples(considering that you want to hit
http://localhost:80/pdf/c-sharp-interview-questions.pdf
in your browser).NOTE: Either use 1st OR use 2nd solution at a time please. Please make sure you clear your browser cache before testing your URLs.
Although, in your example, that is not the only thing that is changing. You would also need to do the following:
career.guru99.com
towww.guru99.com
.top-50-
from the start of the URL-path.-answers/
from the end of the URL-path.Try something like the following instead:
This would redirect a URL of the form:
to:
The
%1
backreference simply captures the "pdf" string from the query string (avoiding repetition) and this is used twice in theRewriteRule
substitution string.The
$1
backreference captures the part of the URL-path after the/top-50-
at the start of the URL-path and before the-answers/
(trailing slash optional) at the end of the URL-path.The
QSD
flag discards the query string from the redirected URL.Test first with a 302 (temporary) redirect before changing to a 301 (permanent) redirect – if that is the intention – so as to avoid potential caching issues.
You will need to clear your browser cache before testing.
Aside:
Where was the
.html
coming from in your example?UPDATE#1: If the last URL-path segment is meant to form the filename as-is with nothing removed (as you seem to imply in comments) then the above rule can be simplified:
(However, this does contradict the example you posted in the question?)
UPDATE#2:
From these two examples there is no single pattern that can be applied that would map the source URL-path to the destination. For example, how would you describe in natural language how you get from
expertadvance-level-qtp-uft-interview-questions
toqtp
and fromtop-35-advanced-software-testing-questions
totesting
?Regular expressions are not magic. There needs to be an identifiable "pattern" that maps from A to B.
So you would need to perform these redirects individually. However, this can be streamlined so that it does not impact site performance. (Although even 100 such redirects written verbatum in
.htaccess
isn’t too bad.)You could internally rewrite requests that match the hostname (
test8.guru99.com
) and query string (format=pdf
) to a script that processes the request and issues the redirect. OR, you could rewrite the request to a (private) subdirectory that contains a second.htaccess
file that only has these specific redirects.For example:
In your root
.htaccess
file, issue an internal rewrite to a subdirectory if it passes preliminary checks:Create the subdirectory
/redirect-pdf
and in a secondary.htaccess
file at/redirect-pdf/.htaccess
create the specific redirects. Note that in theRewriteRule
above I removed the (optional) trailing slash from the rewritten URL, so this should not be checked in the redirects below.You could use a backreference (if applicable) to save repetition if you wanted. For example: