I want /?format=pdf to be replaced with .pdf
Although, in your example, that is not the only thing that is changing. You would also need to do the following:
- Change the hostname from
career.guru99.com to www.guru99.com.
- Remove
top-50- from the start of the URL-path.
- Remove
-answers/ from the end of the URL-path.
Try something like the following instead:
RewriteCond %{HTTP_HOST} ^career\.guru99\.com [NC]
RewriteCond %{QUERY_STRING} ^format=(pdf)$ [NC]
RewriteRule ^top-50-([\w-]+)-answers/?$ https://www.guru99.com/%1/$1.%1 [QSD,R=302,L]
This would redirect a URL of the form:
https://career.guru99.com/top-50-something-here-answers/?format=pdf
to:
https://www.guru99.com/pdf/something-here.pdf
The %1 backreference simply captures the "pdf" string from the query string (avoiding repetition) and this is used twice in the RewriteRule 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:
RewriteRule ^c-sharp-interview-questions.html .......
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:
RewriteCond %{HTTP_HOST} ^career\.guru99\.com [NC]
RewriteCond %{QUERY_STRING} ^format=(pdf)$ [NC]
RewriteRule ^([\w-]+?)/?$ https://www.guru99.com/%1/$1.%1 [QSD,R=302,L]
(However, this does contradict the example you posted in the question?)
UPDATE#2:
https://test8.guru99.com/expertadvance-level-qtp-uft-interview-questions/?format=pdf
https://www.test2.demoguru99.com/pdf/qtp.pdf
https://test8.guru99.com/top-35-advanced-software-testing-questions/?format=pdf
https://www.test2.demoguru99.com/pdf/testing.pdf
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 to qtp and from top-35-advanced-software-testing-questions to testing?
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:
RewriteCond %{HTTP_HOST} ^test8\.guru99\.com [NC]
RewriteCond %{QUERY_STRING} ^format=pdf$ [NC]
RewriteRule ^([\w-]+?)/?$ redirect-pdf/$1 [QSD,L]
Create the subdirectory /redirect-pdf and in a secondary .htaccess file at /redirect-pdf/.htaccess create the specific redirects. Note that in the RewriteRule above I removed the (optional) trailing slash from the rewritten URL, so this should not be checked in the redirects below.
RewriteEngine On
# Set an env var to the base target
# (Just saves some bytes/repetition and easier to update)
RewriteRule ^ - [E=TARGET_BASE:https://www.test2.demoguru99.com/pdf]
# Redirects
RewriteRule ^expertadvance-level-qtp-uft-interview-questions$ %{ENV:TARGET_BASE}/qtp.pdf [R=302,L]
RewriteRule ^top-35-advanced-software-testing-questions$ %{ENV:TARGET_BASE}/testing.pdf [R=302,L]
# etc.
# :
# If get to the end and nothing matched above then fail with a 404...?
You could use a backreference (if applicable) to save repetition if you wanted. For example:
# Redirects
RewriteRule ^expertadvance-level-(qtp)-uft-interview-questions$ %{ENV:TARGET_BASE}/$1.pdf [R=302,L]
RewriteRule ^top-35-advanced-software-(testing)-questions$ %{ENV:TARGET_BASE}/$1.pdf [R=302,L]