URL Rewrite keep %20 in Query String - .htaccess

I am trying to make a rewrite rule to move all pdf's on my site to point to a specific page and then use a Query String as the pdf's current file path to do a look up in a dictionary to see if that url is in my dictionary if it is redirect them to the correct page. The catch is my dictionary of urls has %20 and when I pull the query string it turns the %20 in a space. Thanks for any and all help.
Can you rewrite it to keep the %20 in the query string?
Example URL: /example/example/Big%20Small%20Something%20Pad.pdf
My Rewrite:
RewriteRule ([^/]*)\.pdf$ /redirectPDF.aspx?pdf=$1.pdf [NE,QSA]
Current Query String Output: Big Small Something Pad.pdf
What I want it to look like is Big%20Small%20Someting%20Pad.pdf

Try removing the NE flag. According to ISAPI docs:
http://www.helicontech.com/isapi_rewrite/doc/RewriteRule.htm:
noescape|NE
Don't escape output. By default ISAPI_Rewrite will encode all non-ANSI >characters as %xx hex codes in output.
So it looks like you simply want to omit the NE so that it'll encode the output like it does by default.
RewriteRule ([^/]*)\.pdf$ /redirectPDF.aspx?pdf=$1.pdf [QSA]

Related

post parameters to htaccess sef link

I have url for ex:
http://www.demo.com/a/abcd
And I use this htaccess to post this url's values
RewriteRule ^a/(.*)$ /details.php?sef=$1 [L,NC]
But I need to get new parameters posted the url
For ex:
http://www.demo.com/a/abcd?id=123&qu=11
So how can I get the id and qu variables values via this url without do any changes in URL?
From Apache's docs on mod-rewrite:
Modifying the Query String
By default, the query string is passed through unchanged. You can,
however, create URLs in the substitution string containing a query
string part. Simply use a question mark inside the substitution string
to indicate that the following text should be re-injected into the
query string. When you want to erase an existing query string, end the
substitution string with just a question mark. To combine new and old
query strings, use the [QSA] flag.
Since, you are writing the query string in a rewrite, use the QSA flag:
RewriteRule ^a/(.*)$ /details.php?sef=$1 [NC,QSA,L]

.htaccess redirect home.html to root but allow query strings

I want to 301 redirect home.html to the root to avoid duplicate content. I can do this like this:
RewriteRule ^/home.html$ / [NC,R=301]
However there is a legacy affiliate program that use home.html like this:
home.html?a=companyname
Is there a way to allow the affiliate links, but still 301 home.html?
The query string will be appended automatically. From the docs:
Modifying the Query String
By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.

Rewrite rule for seo - title in url

Lets say I want users to be able to type this url in:
www.website.com/blog/2453/I-gained-0.1%-more-scripting-knowledge-!
I'm trying to include title information in the url for seo benefits.
I also want to include an id for my query. Effectively I want to pick up the id and ignore the title stuff that comes after, bearing in mind its user generated text so could contain any special characters in it.
How can I write a .htaccess rewrite rule so that the server reads it as the following with the appropriate GET data:
www.website.com/blog.php?id=2453
This is what I have tried but frankly I am way out of my depth here:
RewriteRule ^blog/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ blog.php?id=$1 [NC,L]
The rewrite rule you are using should work except for the ., %, and ! characters that are in your URL. The % characters is not safe to use in URLs because it has a special meaning in the URL syntax. I wouldn't use exclamation points either.
If the ID is always going to be numeric, use ([0-9]+) instead of ([A-Za-z0-9-]+).
Try this URL:
www.website.com/blog/2453/I-gained-0.1-more-scripting-knowledge
With this rule:
RewriteRule ^blog/([0-9]+)/[A-Za-z0-9\-\.]+/?$ blog.php?id=$1 [NC,L]

& Ampersand in URL

I am trying to figure out how to use the ampersand symbol in an url.
Having seen it here: http://www.indeed.co.uk/B&Q-jobs I wish to do something similar.
Not exactly sure what the server is going to call when the url is accessed.
Is there a way to grab a request like this with .htaccess and rewrite to a specific file?
Thanks for you help!
Ampersands are commonly used in a query string. Query strings are one or more variables at the end of the URL that the page uses to render content, track information, etc. Query strings typically look something like this:
http://www.website.com/index.php?variable=1&variable=2
Notice how the first special character in the URL after the file extension is a ?. This designates the start of the query string.
In your example, there is no ?, so no query string is started. According to RFC 1738, ampersands are not valid URL characters except for their designated purposes (to link variables in a query string together), so the link you provided is technically invalid.
The way around that invalidity, and what is likely happening, is a rewrite. A rewrite informs the server to show a specific file based on a pattern or match. For example, an .htaccess rewrite rule that may work with your example could be:
RewriteEngine on
RewriteRule ^/?B&Q-(.*)$ /scripts/b-q.php?variable=$1 [NC,L]
This rule would find any URL's starting with http://www.indeed.co.uk/B&Q- and show the content of http://www.indeed.co.uk/scripts/b-q.php?variable=jobs instead.
For more information about Apache rewrite rules, check out their official documentation.
Lastly, I would recommend against using ampersands in URLs, even when doing rewrites, unless they are part of the query string. The purpose of an ampersand in a URL is to string variables together in a query string. Using it out of that purpose is not correct and may cause confusion in the future.
A URI like /B&Q-jobs gets sent to the server encoded like this: /B%26Q-jobs. However, when it gets sent through the rewrite engine, the URI has already been decoded so you want to actually match against the & character:
Rewrite ^/?B&Q-jobs$ /a/specific/file.html [L]
This makes it so when someone requests /B&Q-jobs, they actually get served the content at /a/specific/file.html.

Problems with sorting comments in a SEF url system

I use Commentics for my website and having problem with my SEF urls. I couldn't find a solution in the related forum.
I have a rewrite rule like this :
RewriteRule ^([a-zA-Z0-9_-]+)$ /kurum.php?sef=$1
so my urls like http://fxrehber.com/kurum.php?sef=xtb
turns into this:
http://fxrehber.com/xtb
When I try to sort comments, that does not work
My url look like this :
http://fxrehber.com/xtb?cmtx_sort=5&sef=xtb#cmtx_comments
Is there way to solve this with an extra rewrite rule, or am I in the wrong direction?
Thank you
At last I found the simple solution for that: I just added "[QSA]" at the end of the rule as below:
RewriteRule ^([a-zA-Z0-9_-]+)$ /kurum.php?sef=$1 [QSA]
Source : http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Modifying the Query String
By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.

Resources