.htacces url rewriting

2019-09-11 12:39发布

I have a php/apache website under my userdir, say http://localhost/~johnny5/

I have a /en folder with a .htaccess file and a index.php file :

/home/johnny5/public_html
                         /somefile.php
                         /someotherfile.php
                         /en/
                            .htacces
                            index.php

I enable url rewriting under /en so all the request are handled by index.php. here's the content of .htacces :

Options +FollowSymLinks
RewriteEngine On
RewriteBase /~johnny5/en
RewriteRule . index.php

Up to here, everything works correctly.

When I hit, say, http://localhost/~johnny5/en/foo/bar, the request is handled by /en/index.php.

Now, for testing purpose, I move the site under /var/www so I can acces it directly by http://localhost/. I adjust the .htaccess file to remove the user folder:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /en
RewriteRule . index.php

I now have this structure:

/var/www
        /somefile.php
        /someotherfile.php
        /en/
            .htacces
            index.php

Now, if I hit http://localhost/en/foo/bar, I'd expect the request to be handled by /en/index.php, but all I got is a 404 not found for /en/foo/bar.

It looks like the rewrite module works under my user dir, but not under the "main" dir.

I am missing something?

2条回答
对你真心纯属浪费
2楼-- · 2019-09-11 13:20

So that was all my error - misconfiguration.

I "AllowedOverride" under the wrong folder.

查看更多
唯我独甜
3楼-- · 2019-09-11 13:33

Not sure about your new structure on server, but remember one very important ruler: .htaccess rules works for CURRENT and CHILD (recursivly) directories only, not for dirs which are higher then .htaccess dir.

So if your .htaccess is now in:

/home/johnny5/public_html/en/.htaccess

it can't work with path

http://localhost/en/foo/bar

if your http://localhost/ root on server is home/

Try to move your htaccess to the root dir of your www server and try again

查看更多
登录 后发表回答