I have lot of URL that are having www.example.com/info/range/-string or www.example.com/info/range/ string, I want to remove white space of - in start of the string, how I can do this with .htaccess, I have tried this method but not working for me.
How to redirect %20 or White space automatically to + or - with htaccess?
If you want to remove hyphen or space before string and after info/range/ then you can use this redirect rule:
RewriteEngine On
RewriteRule ^(info/range)/[-\s]+(.+)$ /$1/$2 [L,NC,R=302]
I would also advice you to update the question with clear examples and your current .htaccess.
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.
I'm trying to do this by using .htaccess
Redirect: http://example.com/en/signup?type=something
To: http://example.com/snapshots/en_signup_type=something.html
Basically, convert all slashes and question marks to underscores and appending .html.
I found out how to do, there are actually a few more steps than I thought.
Replace all slashes with underscores, RewriteRule ^(.*)\/(.*)$ $1_$2 [R=301,L], based on the number of slashes we have, it will redirect the same amount of times, one redirection will convert one slash to one underscore.
http://example.com/en/signup?type=something -> http://example.com/en_signup?type=something
Convert the query string (change question mark to underscore)
RewriteRule ^(.*)$ http://example.com/snapshots/$1_%{QUERY_STRING}.html? [P,L]
The question mark after .html is to prevent the query string to be appended again.
I want a redirection in my website.
The old url looks like this :
http://www.example.com/Prem_Ratan_Dhan_Payo/Prem_Leela.html
Now we have to 301 redirect it to
http://example.com/songs/prem-leela/
In total I have to
Check if the url end with .html then we will redirect it other wise let it pass.
remove .html from end.
make all letters in lower case.
change underscore to hyphen.
change second segment to 'songs'.
Please help me.
This should get you started:
RewriteEngine On
# Replace underscores with hyphens in all requests ending in .html.
# The rule replaces one char at a time and is repeated until no matches are found.
RewriteCond "%{QUERY_STRING}" "\.html$" [NC]
RewriteRule ^([^_]*)_(.*) $1-$2 [N]
# Capture the file name without extension inside braces
# Use a lower-case mapping on the file name.
# Destination is /songs/filename
RewriteMap lc int:tolower
RewriteRule ^.*/(.*)\.html$ /songs/${lc:$1} [NC, R=301]
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