• Hi,
    I wonder is anyone could help me out. I have installed a copy of WordPress locally via XAMPP. I need to be able to rewrite hundreds of urls with different ID’s and redirect them to a different sun directory and add in a file extension.

    This is what I have :

    RewriteCond %{QUERY_STRING} ^type=pdf&id=(.*)$
    RewriteRule /?$ /pdf/%1.pdf [L]

    This is an example of the entry URL :

    https://localhost/wordpress/?type=pdf&id=id00264

    and this is what I need :

    https://localhost/wordpress/pdf/id00264.pdf

    I don’t understand why this doesn’t work ?? Any help would be very much appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Dion

    (@diondesigns)

    I don’t know if there are any other issues, but you must escape special characters in regular expressions. Try this for your rewrite condition and see if it helps:

    RewriteCond %{QUERY_STRING} ^type\=pdf\&id\=(.*)$
    
    Thread Starter Serensites

    (@serensites)

    Thank you for your reply but that doesn’t seem to help.

    I am running the site locally via XAMPP. I’ve checked the httpd.conf file and eveything seems fine. If I click on a link to :

    https://localhost/wordpress/?type=pdf&id=id00264

    I don’t get redirected to

    https://localhost/wordpress/pdf/id00264.pdf

    but instead to the home page (the url doesn’t change)

    Here is my .htaccess file :

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /wordpress/
    
    RewriteCond %{QUERY_STRING} ^type=pdf&id=(.*)$
    RewriteRule ^(.*)$  pdf/%1.pdf [R=301]
    
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /wordpress/index.php [L]
    </IfModule>
    
    # END WordPress
    Dion

    (@diondesigns)

    I can guarantee that if you don’t make the changes I previously mentioned, your rewrite condition will never activate the rewrite rule. Special characters like = and & must be escaped with a backslash to be treated as normal characters.

    Now…after seeing the full file and noticing that it is different than what you originally posted, I might suuggest the following change:

    RewriteRule ^(.*)$  pdf/%1.pdf [L,R=301]
    

    Without the L in the rewrite rule, Apache will continue checking rewrite conditions, and you will wind up on the index.php page since it’s the last rule.

    You may also need to specify the full URL (including the https:// part) in order to get a 301 redicrect to work properly. And since you’re dealing with 301 redirects, after making changes to .htaccess, make sure to completely clear your browser cache before checking the site.

    Thread Starter Serensites

    (@serensites)

    Thank you so much. Sorry, I posted the first line without the changes I made with the backslashes.
    Nothing seems to be happening, it’s driving me crazy :(. I also tried testing a basic rewrite from one page to another but that doesn’t work either. I commented out my rewrite conditions to make sure it wasn’t interfering but still nothing happens.
    The wordpress permilinks work if I change them so logically my httpd.conf is set up. Do you see any other factor which might be blocking my rewrite rules?

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /wordpress/
    
    #RewriteCond %{QUERY_STRING} ^type\=pdf\&id\=(.*)$
    #RewriteRule ^(.*)$  https://localhost/wordpress/pdf/%1.pdf [R=301]
    
    Rewrite /ebooks/ https://localhost/wordpress/sample-page/
    
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /wordpress/index.php [L]
    
    </IfModule>
    
    # END WordPress
    Dion

    (@diondesigns)

    Try the following. If this code is in your httpd.conf file, you must restart Apache in order for the changes to take effect.

    I commented out your /ebooks/ rule because there is no Rewrite directive in Apache. Alias, Redirect, or RewriteRule could be the correct directive, but that would depend on what you are trying to accomplish.

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /wordpress/
    
    RewriteCond %{QUERY_STRING} ^type\=pdf\&id\=(.*)$
    RewriteRule ^(.*)$  https://localhost/wordpress/pdf/%1.pdf [R=301,L]
    
    #Rewrite /ebooks/ https://localhost/wordpress/sample-page/
    
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /wordpress/index.php [L]
    
    </IfModule>
    
    # END WordPress
    
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘RewriteCond with id variable not working’ is closed to new replies.