Forum Moderators: phranque

Message Too Old, No Replies

Redirect from WP subfolder back to root

Hacking the .htaccess to redirect wordpress

         

rschwab

8:01 pm on Sep 1, 2010 (gmt 0)

10+ Year Member



Hello!

I have a drupal installation in my domain root, with wordpress running under the /blog subdirectory. This was all fine and well until I tried using the drupal blog module, which tries to use that same /blog subdirectory, only to get the request lost in the wordpress scripts.

The specifics:
  • I have my wordpress URL's set up to use the year as the first part of the path past blog, for example: http://www.example.com/blog/2010/this-is-a-blog-post
  • Drupals blog module will use the same spot for the year, but populate it with userid data, example for user 1: http://www.example.com/blog/1
  • My goal is to have all URLs with a number in that 3rd URL part route back to http://www.example.com/index.php so Drupal can bootstrap it
  • The exceptions being the numbers 2010-2020 (those users are just out of luck) - I want those reserved for the next 10 years worth of wordpress blogging.

The solution is to hack up the .htaccess file in my wordpress directory.

Here is my ham-handed attempt at doing so:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
# Here I try to route any URL with numbers after /blog/
RewriteRule ^\d+ http://www.example.com/index.php [L]
# Here I try to send 2010-2020 back to /blog/index.php
RewriteRule ^(201\d | 2020)/* http://www.example.com/blog/index.php [L]
</IfModule>

# END WordPress


I feel like I'm not doing this quite right, and want to be sure before I put this on a live server. Can anyone verify and help point me towards fixing any broken parts?

Thanks so much!
-Ryan

jdMorgan

9:22 pm on Sep 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PCRE patterns will only work on Apache 2.x.

You have syntax errors due to the spaces in your patterns. mod_rewrite code is not free-format, and literal spaces must be escaped. Unescaped spaces terminate the 'fields' of the directives, and may not be used for pretty-code formatting.

I don't know why you're using the RewriteBase directive, unless your server config is weird.

This code will need to go into your top-level .htaccess if you wish to 'steer' requests to either /index.php or /blog/index.php

Do not redirect to http://www.example.com/blog.index.php unless you have already corrected all of those URLs and no longer link the "2000" URLS to /blog/2000. I suggest an internal rewrite, which can only be done (in this case) if the code is located in example.com/.htaccess

You might consider keying off both the numeric range and the lack of further URL-path info past the numbers... Saying that userIDs between 2010 and 2020 are "out of luck" sounds a bit too cavalier for my taste.

Frankly, you'd do better to consider re-installing these blogs into separate and explicit subdirectories -- or even into separate subdomains.

Jim

rschwab

10:34 pm on Sep 1, 2010 (gmt 0)

10+ Year Member



Thanks Jim! I've re-written the blog module to use a different path like you suggest.

The RewriteBase directive comes from the standard wordpress install - with the exception of the last two RewriteRule statements that is the out-of-the-package wordpress .htaccess file.

- Ryan

jdMorgan

1:01 am on Sep 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah, I've seen their code before, but usually without the RewriteBase. There are other problems with that code that usually cause poor performance if your site is busy and on a shared server.

See Reducing Costly Rewrites in Wordpress [webmasterworld.com] in our Apache Forum Library for a quick way to speed up your WP performance. You can likely use the same technique for other applications that use a similarly-poor rewrite.

Jim