简体   繁体   中英

.htaccess to redirect everything to index.php, but keep a copy of the website in a subdirectory

I have a simple .htaccess file. I want it to redirect everything that's not a file or directory to index.php. I am keeping another copy of the website in the subfolder may9-new (with the same .htaccess file copied into that directory). But the problem is, I can access www.example.com/may9-new successfully, but if I go to www.example.com/may9-new/anything it appears that I am seeing the version of the site in the root folder. Here's the .htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/may9-new
RewriteRule ^([^?]*)$ /index.php?path=$1 [NC,L,QSA] 
</IfModule>

I barely know anything when it comes to .htaccess, so bear with me. I learned that RewriteCond is a condition for which RewriteRule should take place. So I tried to write a condition that the path can't start with /may9-new . This RewriteCond works only for www.example.com/may9-new and not if there are more slashes added.

Can someone please help me figure out the correct rule?

Thanks!

Don't use absolute path / in target. This is the .htaccess you can keep in root as well as in sub-directory:

DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on

RewriteRule ^index\.php$ - [L,NC]

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# route to index.php in current directory
RewriteRule ^(.+)$ index.php?path=$1 [L,QSA] 
</IfModule>

Maybe you need to based your configuration to your host system. Try to check these:

RewriteCond %{REQUEST_URI} !^/may9-new(.*)
RewriteRule ^(.+)$ /index.php?path=$1 [NC,QSA]

You can also contact your hosting provider about this issue as you've said:

The weird thing is, this site with its .htaccess file works on a different domain hosted by...

Do you also have the same .htaccess file in your "may9-new" sub-folder?

I am keeping another copy of the website in the subfolder may9-new ( with the same .htaccess file copied into that directory ).

If you also have, then there maybe a conflict, try to modify the .htaccess file of your sub-folder:

RewriteRule ^(.+)$ /may9-new/index.php?path=$1 [NC,QSA]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM