skip to Main Content

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


  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.

    Login or Signup to reply.
  2. 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]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search