1

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

4
  • 2
    Your rule doesn’t match the requested path here in the first place. (Or your problem description is not acurate.) Commented Jan 6, 2021 at 14:29
  • I have Redirect on Following Links - test8.guru99.com/top-50-c-sharp-interview-questions-answers/… But It was not redirecting correctly it's 404 error. Redirect correct link is - test2.demoguru99.com/pdf/c-sharp-interview-questions.pdf Commented Jan 20, 2021 at 8:06
  • I have used following code RewriteCond %{QUERY_STRING} format=pdf [NC] RewriteRule ^c-sharp-interview-questions.html /pdf/c-sharp-interview-questions.pdf? [R=301,L] Commented Jan 20, 2021 at 8:06
  • @anubhava, yes changes on host name Commented Jan 21, 2021 at 6:12

2 Answers 2

1

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.

RewriteEngine ON
RewriteCond %{HTTP_HOST} ^career\.guru99\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]

RewriteCond %{QUERY_STRING} ^format=(.*) [NC]
RewriteRule ^top-50-(.*)/?$ pdf/c-sharp-$1.%1 [NC,L]


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).

RewriteEngine ON
RewriteCond %{HTTP_HOST} ^career\.guru99\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]

RewriteRule ^(pdf)/([^.]*)\.(pdf)/?$ top-50-$1/?format=$2 [NC,L]

NOTE: Either use 1st OR use 2nd solution at a time please. Please make sure you clear your browser cache before testing your URLs.

Sign up to request clarification or add additional context in comments.

12 Comments

I have try to following solution but it was not work. I have Redirect on Following Links - test8.guru99.com/top-50-c-sharp-interview-questions-answers/… But It was not redirect on 404 error. Redirect correct link is - test2.demoguru99.com/pdf/c-sharp-interview-questions.pdf
@renishbhalodiya, could you please do let me know if you are getting any errors?
@renishbhalodiya, ok could you please do give some examples of URLs here?
@RavinderSingh13 Although in the question they sate they "have 100+ links". These are presumably just 2 of those "100+ plus"!
@RavinderSingh13 Yeah, and I'm not seeing a pattern here?! ;)
|
1

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:

  1. Change the hostname from career.guru99.com to www.guru99.com.
  2. Remove top-50- from the start of the URL-path.
  3. 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]

4 Comments

Our Url was not fixed on "top-50" words. for ex.- career.guru99.com/something-here/?format=pdf and 100+ links will redirect
So, how do you identify the target URL from the source URL? In the single example you posted in the question the only discernable difference would seem to be the removal of top-50- and -answers/, as I stated in my answer above. Are you now suggesting that there are different prefixes that should be removed? How do you identify a "prefix"? Or is the URL-path meant to remain the same? But then that contradicts with the example you posted in the question?
There is no discernable pattern in those two redirects, so you would need to perform these redirects separately. However, this can be streamlined so that it does not impact performance and so that you don't need to repeat the RewriteCond directives for each redirect. I've updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.