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]
Related
So here's my problem. I took over a site that has has a bunch of pages indexed that have %20 indexed in Google. This is simply because the person decided to just use the tag name as the title and url slug. So, the urls were something like this:
http://www.test.com/tag/bob%20hope
http://www.test.com/tag/bob%20hope%20is%20funny
I have added a new field for the url slug and string replaced all spaces with dashes. While I have no problem linking to these new pages and getting the data, I need to 301 redirect the old URLs to the new URLs, which would be something like:
http://www.test.com/tag/bob-hope
http://www.test.com/tag/bob-hope-is-funny
So, it needs to be able to account for multiple spaces. Any questions? :)
Use these rules in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
# keep replacing space to hyphen until there is no space use internal rewrite
RewriteRule ^([^\s%20]*)[\s%20]+(.*)$ $1-$2 [E=NOSPACE:1]
# when there is no space make an external redirection
RewriteCond %{ENV:NOSPACE} =1
RewriteRule ^([^\s%20]+)$ $1 [R=301,L]
This will replace all space characters (\s or %20) to hyphen -
So a URI of /tag/bob%20hope%20is%20funny will become /tag/bob-hope-is-funny with 301
Brief Explanation: If there are more than 1 space in URI then 1st RewriteRule is fired recursively replacing each space character with hyphen - until there is no space left. This rule will only rewrite internally.
Once no space is left 2nd RewriteRule is fired which just uses a 301 redirect to the converted URI.
Building on #anhubhava's answer, it's close, but that will also match %,2 or 0 in the URL, and it can cause a loop on apache 2.2 if you don't use the DPI parameter. The full script should look like this:
Options FollowSymlinks MultiViews
RewriteEngine on
RewriteBase /
# keep replacing space to hyphen until there is no space use internal rewrite
RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1-$2 [N,E=NOSPACE:1,DPI]
# when there is no space make an external redirection
RewriteCond %{ENV:NOSPACE} =1
RewriteRule ^([^\s%20]+)$ $1 [R=301,L]
I've also added the N (Next) parameter as this then forces the rules to be re-evaluated from the start straight after this rule if it matches. If this isn't there, you can get problems if you're using apache as a reverse proxy as it's unlikely that it'll get to the end of the rewrites before something else happens.
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 little problem. I'm using htaccess for more userfriendly url's but when i add some whitespaces in URL it gives me the next error:
The requested URL /capitole/Limba Engleza was not found on this server.
My htaccess code looks like that:
<ifModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule nota.jpg php/img_nota.php [R=301]
RewriteRule ^login login.php
RewriteRule ^recuperare recuperare.php
RewriteRule ^inregistrare inregistrare.php
RewriteRule ^/?([\sa-zA-Z0-9_-]+)(/?([a-z0-9=]+)(/=)?([a-z0-9=]+)?)?$ index.php?page=$1&par1=$3&par2=$5 [NC,L]
</ifModule>
Can someone help me?
The URL can not contain whitespaces. Whitespaces in URLs are encoded using the percentage (%).
If it is a query string (after ?) it's common to use the operator + (for example: big+ships).
If the string is in the path of the URL, then %20 is used (for example: big%20ships).
As detailed on the RFC: rfc2396
The space character is excluded because significant spaces may
disappear and insignificant spaces may be introduced when URI are
transcribed or typeset or subjected to the treatment of word-
processing programs. Whitespace is also used to delimit URI in many
contexts.
space = US-ASCII coded character 20 hexadecimal
your 2nd segment doesn't accept space modify it to read as follows.
RewriteRule ^/?([\sa-zA-Z0-9_-]+)(/?([\sA-Za-z0-9=]+)(/=)?([a-z0-9=]+)?)?$ index.php?page=$1&par1=$3&par2=$5 [NC,L]
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]
I am trying to write a rule which says
xyz.com/hotels_in_someplace will direct to xyz.com/test.php?place=someplace
by using the following in my htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^hotels_in_([a-z]+)$ test.php?place=$1 [L]
but its not working and I cant figure out why, I am running a wamp server as a dev. when i tried to run xyz.com/test.php?place=someplace (without the htaccess in the directory) it works but I suspect there's something my rule which is wrong.
additional:
whoops....my dumb ass mistake, mod_rewrite wasnt enabled...
Try it without the end-line anchor ($)
RewriteRule ^/hotels_in_([a-z]+) /test.php?place=$1 [L]
Also, did you allow the .htaccess files to be parsed?
Update: I didn't notice the RewriteBase in the question before so my point about the leading slash is null. However the RewriteRule does work on my machine.
Try:
RewriteRule ^hotels_in_([a-z]+)$ /test.php?place=$1 [L]
The path to test.php needs the leading slash. The way you are doing the rewrite you will be redirected to xyz.com/path/to/web/docs/on/machine/test.php and thats not what you want. :)
Also if you are you including a trailing slash in your test.
The above rule will not match xyz.com/hotels_in_someplace/.
To match the leading slash this should work:
RewriteRule ^hotels_in_([a-z]+)/?$ /test.php?place=$1 [L]
Your example works fine for me. Might want to make a few little adjustments (see my example, which captures uppercase letters, hyphens and underscores (for hotels in countries with more than one word (united-states, united_states)) and a trailing forward slash).
RewriteRule ^hotels_in_([A-Za-z_-]*)(/?)$ /test.php?place=$1
Hope that helps.