0

I am trying to rewrite a URL to remove two subdirectories and display the page in the root directory instead. This the code I use in my .htaccess:

RewriteRule ^page$ subdirectoryone/subdirectorytwo/page [NC,QSA,L]

This is my complete .htaccess file in the root directory:

Options +FollowSymLinks

RewriteEngine on

RewriteCond %{HTTPS_HOST} ^mysite.com
RewriteRule (.*) https://www.mysite.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^mysite.com
RewriteRule (.*) https://www.mysite.com/$1 [R=301,L]

RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://www.mysite.com/$1 [R=301,L]

# remove trailing slashes
RewriteRule (.*)/$ $1 [L,R=301]

RewriteRule ^page$ subdirectoryone/subdirectorytwo/page [NC,QSA,L]

This is the .htaccess in the first subddirectory:

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

# END WordPress

However, when I open the URL www.mysite.com/page it displays an error 404. By the way: I am using Wordpress, installed in the first subdirectory – does that make any difference?

Also, it works when I only remove the first of the two subdirectories.

Any ideas? Thanks in advance!

4

2 回答 2

0

It's probably got something to do with the way wordpress is extracting the path info and request URI information. If you request /page and even if the rewritten request gets to wordpress, wordpress may be looking at the request URI and see "/page" and assume that's a 404.

I'm sure there's some wordpress specific way of handling these sort of things, but if you have mod_proxy loaded, you can try adding a P flag in your rule:

RewriteRule ^page$ /subdirectoryone/subdirectorytwo/page [NC,QSA,L,P]

So that the request /subdirectoryone/subdirectorytwo/page goes through the entire request handling process in it's own request.

于 2013-07-10T21:46:16.907 回答
0

I didn't actually solve this problem but I finally found a workaround for what I want to do:

I installed the WordPress plugin 'Permalink Editor' which allows me to change the URL from

www.mysite.com/subdirectoryone/subdirectorytwo/page

to

www.mysite.com/subdirectoryone/page

Then, I added the following to the .htaccess in the root directory:

RewriteRule ^page$ subdirectoryone/page [NC,QSA,L]

This way, I can access the page www.mysite.com/subdirectoryone/subdirectorytwo/page from the URL www.mysite.com/page.

Lesson learned: In WordPress, always look for a plugin before asking stupid questions.

于 2013-07-11T22:30:47.563 回答