homepage Welcome to WebmasterWorld Guest from 54.197.111.87
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
301 Redirect URL with space in Word Press
How to redirect a URL with space and encoded URLs in WordPress via htaccess
pepsimoz




msg:4279431
 7:28 am on Mar 10, 2011 (gmt 0)

I've been having a pesky problem trying to redirect a URL that has a space in it.

Below is my setup for redirects in .htaccess file.

* The www to non-www works well, so no problems there
* Other page redirects work well, too --> example:
redirect 301 /some-page/ http://example.com/another-page/ (I didn't post those because I have a few too many : )

Redirect(s) in question are the last four.
Here is my setup:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

redirect 301 /archives/10-college-&nbsp;majors/ http://example.com/archives/10-college-majors/
redirect 301 /archives/10-college-%20majors/ http://example.com/archives/10-college-majors/
redirect 301 /archives/10-college-%C2%A0majors/ http://example.com/archives/10-college-majors/
redirect 301 "/archives/10-college- majors/" http://example.com/archives/10-college-majors/


Redirects not working... As you can see I've tried using other variations of the "space" but no go. I also used a redirect in cPanel's Redirect Menu, but maybe omitted something due to my limited knowledge. I deleted the cPanel redirects.

Note: the URL displays with a space in the address bar of all major web browsers: http://example.com/10-college- majors/ and goes to a 404 page

the &nbsp; redirect (in .htaccess above) works when inserted directly into address bar. Even so, when looking at source of page linking to mine, the URL looks like this:

http://example.com/archives/10-college-&nbsp;majors/

Clicking the URL in Source View in FireFox takes me to

http://example.com/archives/10-college-%C2%A0majors/

this is confirmed by Google Webmaster Tools and AWStats. Both services use the above mentioned URL, with %C2%A0, that causes the 404 error from linking sites.

I don't have any redirects, besides simple page to page redirects (mentioned in the beginning). No permalink plugins.

The URL is linked from several reputable websites, but because it was copied from one source all the websites have the URL wrong. I tried contacting the webmasters, but to no avail. Is there a clean way to redirect ONLY this problematic URL without compromising other redirects, etc?

Any ideas would be great. Thanks in advance.

 

g1smd




msg:4279475
 8:51 am on Mar 10, 2011 (gmt 0)

Never mix Redirect and RewriteRule in the same .htaccess file.

Change the Redirect rules to use RewriteRule with the [R=301,L] flags and move them to the top of the file.

The space between two\ words in the pattern needs to be escaped.

See other recent threads about optimising the default Wordpress rewrite code. Currently every request to your server invokes two very slow and inefficient disk reads. The -f and -d "exists" checks should be prefaced by another RewriteCond limiting when those checks really need to be done.

pepsimoz




msg:4279871
 8:39 pm on Mar 10, 2011 (gmt 0)

Change the Redirect rules to use RewriteRule with the [R=301,L] flags and move them to the top of the file.


Like this?
RewriteRule ^/archives/10-college-\ majors/ http://example.com/archives/10-college-majors/ [R=301,L] <-- code would be before all else in the .htaccess file

I'm not sure which method of representing the "space" is the method. Is it the
/ with space or &nbsp; or %C2%A0 or something entirely different? I said I can do simple redirects, but it seems even those aren't proper :-)

At this time (2:30pm CT) I don't want to make any changes as to not time out the website and inconvenience visitors and users, but I will try it later; hopefully with some refined help.

Thank you for the reply.

g1smd




msg:4279893
 9:20 pm on Mar 10, 2011 (gmt 0)

Yes. The "one line" redirects for single URLs look good.

The code for two words with a space between them is: "
two\ words"

That's it. So shockingly simple it hurts. You might also want to look for %20 and the like and it probably would harm things to do so: "
two(\ |%20)words"
pepsimoz




msg:4280075
 6:08 am on Mar 11, 2011 (gmt 0)

Tried this, but site timed out (500 Server Error):

RewriteRule ^/archives/10-college-\ majors/ http://example.com/archives/10-college-majors/ [R=301,L]

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress


Also tried this:

RewriteRule ^/archives/10-college-(\ |%20)majors/ http://example.com/archives/10-college-majors/ [R=301,L]

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress


Notice: I removed the other versions of the redirect to test it. I also tried the rewrite directive for the URL with space under the Word Press redirect, just to rule things out.

I also tested the rewrite URL with &nbsp; and %C2%A0 in it. All rendered a server error.

g1smd




msg:4280099
 7:07 am on Mar 11, 2011 (gmt 0)

Ooops. I missed a very small but important detail.

The there should be no leading / in the rule pattern if this code is going in the .htaccess file

^/words - incorrect
^words - correct

pepsimoz




msg:4280100
 7:14 am on Mar 11, 2011 (gmt 0)

I did as you said. Still no go --> it times out 500

g1smd




msg:4280103
 7:20 am on Mar 11, 2011 (gmt 0)

RewriteEngine On needs to be the very first line of code and must appear only once (you have two). Should have noticed that before, but was posting via Wifi from the pub!

You can likely drop both of the <ifModule> lines too.

[edited by: g1smd at 7:37 am (utc) on Mar 11, 2011]

pepsimoz




msg:4280104
 7:36 am on Mar 11, 2011 (gmt 0)

It's there only once. I just posted two different scenarios.

the <ifmodule> is done by Wordpress I wouldn't know where to cut off what.

I tried putting the rewrite rule after
RewriteEngine On (in different orders).

RewriteEngine On
RewriteRule ^archives/10-college-(\ |%20)majors/ http://example.com/archives/10-college-majors/ [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress


500 Server Error

pepsimoz




msg:4280106
 7:38 am on Mar 11, 2011 (gmt 0)

You're right. it's there twice with the Wordpress one.

pepsimoz




msg:4280107
 7:42 am on Mar 11, 2011 (gmt 0)

I tried this.. times out

RewriteEngine On
RewriteRule ^archives/10-college-(\ |%20)majors/ http://example.com/archives/10-college-majors/
[R=301,L]RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

pepsimoz




msg:4280108
 7:45 am on Mar 11, 2011 (gmt 0)

Also tried this


RewriteEngine On
RewriteRule ^archives/10-college-(\ |%20)majors/ http://example.com/archives/10-college-majors/
[R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]


The RewriteBase / might have been off. 500 Server Error.

g1smd




msg:4280109
 7:51 am on Mar 11, 2011 (gmt 0)

Are the New Lines just for forum posting formatting?

RewriteRule pattern target [flags] -- should all be on one line.
pepsimoz




msg:4280110
 8:00 am on Mar 11, 2011 (gmt 0)

the [R=301,L] are definitely on the same line. I even put everything on separate lines. Perhaps it's the forum formatting that's cutting it off there.

RewriteEngine On
RewriteRule ^archives/10-college-(\ |%20)majors/ http://example.com/archives/10-college-majors/ [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]


500 Server Error

pepsimoz




msg:4280111
 8:02 am on Mar 11, 2011 (gmt 0)

g1smd, thank you for your great help. I really appreciate it -- to all others reading, still no solution.

pepsimoz




msg:4280514
 1:54 am on Mar 12, 2011 (gmt 0)

I ended up downloading the Redirection plugin for WordPress. Worked - it changes the database to the best of my knowledge.

I still wish I could have done it without it, but I've spend over 3 full working days on trying to figure it out. As they say: "Knowledge is Power" and "Time is a Commodity" - I had to pick the latter.

g1smd, thank you very much for the great help.

jdMorgan




msg:4283350
 10:35 pm on Mar 17, 2011 (gmt 0)

The "%" in "%20" must be escaped. Otherwise, it is taken as "back-reference to the value of local variable named %2" followed by a literal zero. Back-references in patterns are not allowed, resulting in a 500 error.

When you get a 500-Server error, check your server error log.

Also, be sure to completely delete your browser cache after changing any server-side code.

Jim

pepsimoz




msg:4283353
 10:45 pm on Mar 17, 2011 (gmt 0)

Thank you for the explanation.. just so the newbie in me is clear, escaped means:

/some-%\ 20url/ or am I making more of a mess here?

g1smd




msg:4283366
 11:11 pm on Mar 17, 2011 (gmt 0)

Ack. It was an obvious error I should have spotted - but didn't. The
|%20) should be |\%20) here:

RewriteRule ^archives/10-college-(\ |\%20)majors/ http://example.com/archives/10-college-majors/ [R=301,L]


Don't forget to add the non-www to www code back in here as well.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved