I need to rewrite a url that contains a comma to another URL. The original URL also contains +'s (pluses) which I have figured out need to be escaped with a backslash, but the commas don't seem to be read properly.
Here's what it looks it like:
RewriteRule ^locations/New+York/Buffalo,\+Erie\+County,\+and\+Surrounding\+Areas$ "/locations/New+York/Buffalo" [R=301,NC]
I almost got it right, you only forgot to escape the first +:
RewriteRule ^locations/New\+York/Buffalo,\+Erie\+County,\+and\+Surrounding\+Areas$ "/locations/New+York/Buffalo" [R=301,NC]
Related
Someone linked to my site from nice place but the moron type the wrong URL, he did %20 at the end of it (probably his site did that). So I want to redirect http://example.com/%20 to http://example.com, because http://example.com/%20 is of course going to http://example.com/404 cuz that one is not valid URL.
I tried this:
rewriterule ^%20(.*)$ http://example.com$1 [r=301,nc]
But it just doesn't work. Looks like the % character has to be somehow escaped or something. So i tried also
rewriterule ^\%20(.*)$ http://example.com$1 [r=301,nc]
to give the % its original meaning by escaping it but it doesn't seem to be working either. Also read htaccess to escape percent (%) from URL but the solution presented there doens't seem to be working either. Anyone has idea how to do this? Thanks so much.
You can match %20 in RewriteRule using \x20:
RewriteRule ^\x20(.*)$ /$1 [R=301,L,NE]
I have a query string like
search.php?id=12&keyword=abc&api=gIUTG6898
And I want the URL to be like this:
search/?id=12&keyword=abc&api=gIUTG6898
Now I found a lot of solutions but they are limited to only one variable in the query string. Thanks in advance
It's not that complicated, since you just want to 'transfer' the query string. You can ignore it in the RewriteRule.
RewriteRule ^search/?$ search.php [QSA]
This just rewrites 'search/' to 'search.php'.
Since you are wanting to remove the .php and replace it with a slash, you'd need to reverse Floern's RewriteRule somewhat:
RewriteRule ^/search.php$ /search/ [QSA,L]
I'm having some issues with redirecting some pages with "%11" and "%28".
I'm trying to redirect a couple of pages, the rest work but I realized those with some symbols in it are not redirecting.
For example:
Redirect 301 /cars/mercedes%11benz/ http://www.example.com/cars/mercedes-benz/
Redirect 301 /alfa-romeo/alfa-romeo-147-%282001%E2%80%932009%29-2008090174/ http://www.example.com/cars/alfa-romeo-147/
do not work.
Thanks in advance for the help.
Basically specifies that only ASCII text is allowed. Might u having white spaces in url. Please remove them.
As for mod_rewrite, I believe that you've got that right except that the dot character in the character range need not be escaped, i.e., the hyphen is properly located at the beginning and the space is escaped. The ΓΌ probably doesn't need to be escaped but it shouldn't hurt). As for browsers making the conversion, that's a "browser thing" which Apache understands (and converts internally to the correct character).
Try this:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^%?\ ]*\%
RewriteRule ^. http://www.example.com/ [R=301,L]
RewriteRule ^/cars/mercedes-benz/ http://www.test-site.com/cars/mercedes%11benz/ [QSA]
I want to allow special charecters in URL
my htaccess code is
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^blog/?$ SocialNetwork/blog.php [L]
RewriteRule ^blog/(\d+)/?$ SocialNetwork/blog.php?id=$1 [L]
RewriteRule ^blog.php/(\d+)/?$ SocialNetwork/blog.php?id=$1 [L]
RewriteRule ^blog.php/(\d+)/([A-Za-z0-9-_[]]+)/?$ SocialNetwork/blog.php?id=$1&title=$2 [L]
</IfModule>
it works for
blog/1
blog/1/hii
blog/1yooo_title
but does not work for
blog/1/[hii-this is [] title
In your last rewrite rule, try escaping the square brackets. Also if you need to match spaces you need to include \s in there:
^blog.php/(\d+)/([A-Za-z0-9-_\[\]\s]+)/?$
Or perhaps consider the simpler which accepts anything in the title:
^blog.php/(\d+)/(.+)/?$
Another point is that you should not have spaces in your URLs. They should be escaped to "+" or %20. So depending on this your regex would change, except the last one I proposed should work.
I'm pretty sure that you can't use spaces in a URL. Ever.
only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL.
http://www.ietf.org/rfc/rfc1738.txt
With regards to the forward slash "/" when giving a regex to RewriteRule or RewriteCond, or anything else related to .htaccess in particular, is there a need to escape the forward slash?
Here is an example of what I am trying to achieve
RewriteEngine on
RewriteOptions inherit
RewriteBase /uk-m-directory/
RewriteRule ^(region|region\/|regions\/)$ regions [R=301,L]
RewriteRule ^(county|county\/|counties\/)$ counties [R=301,L]
RewriteRule ^(city|city\/|cities\/)$ cities [R=301,L]
The above works fine, and it continues to work fine when I remove the backslashes as shown below
RewriteEngine on
RewriteOptions inherit
RewriteBase /uk-m-directory/
RewriteRule ^(region|region/|regions/)$ regions [R=301,L]
RewriteRule ^(county|county/|counties/)$ counties [R=301,L]
RewriteRule ^(city|city/|cities/)$ cities [R=301,L]
Which one is the correct way? Are they both wrong?
Is there any special reason the forward slash should be escaped, or shouldn't?
My guess is that the forward slash does not need to be escaped because it isn't a special character, as far as I know. But I just want to be sure.
In case you're wondering the point of this code, it redirects city, county, and region (with or without a forward slash) to their plural equivalents. Furthermore if the plural has a forward slash it removes the forward slash.
No, you do not have to escape slashes. Forward slashes don't have any special meaning in regular expressions.
The one common character that has bitten me in the past is ? in query strings. That one you do have to escape.