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.
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 need your help regarding redirect rules.
I have an old URL structure:
www.example.com/en/news/news/single/article/SPECIFIC-ARTICLE/
(with SPECIFIC-ARTICLE being a placeholder for a whole bunch of articles - with a trailing slash!).
After the relaunch the name and structure of the folders will change to:
www.example.com/en/news/SPECIFIC-ARTICLE
So all articles (with trailing) in the old structure should be redirected to the corresponding articles in the new structure. And shouldn't have a trailing slash anymore.
How should the mod_rewrite rule in .htaccess look like?
And how would this rule change if there wasn't a trailing slash but a .html at the end of the old article URLs?
Using mod_rewrite in .htaccess this would be something like (near the top of the file):
RewriteEngine On
RewriteRule ^(en/news)/news/single/article/([\w-]+)/$ /$1/$2 [R=302,L]
$1 is a backreference to the en/news match in the RewriteRule pattern (this is simply to avoid repetition). $2 is a backreference to the SPECIFIC-ARTICLE (which is assumed to consist of just the letters A-Z, a-z, 0-9, _ and -).
If there is .html instead of the trailing slash, then change the RewriteRule pattern to:
RewriteRule ^(en/news)/news/single/article/([\w-]+)\.html$ /$1/$2 [R=302,L]
Note the dot is backslash escaped to match a literal dot and not any character.
Note that this is a temporary (302) redirect, change this to a 301 (permanent) redirect only when you are sure it is working OK.
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.
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'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]