简体   繁体   English

.htaccess将所有内容重定向到index.php,但将网站的副本保留在子目录中

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

I have a simple .htaccess file. 我有一个简单的.htaccess文件。 I want it to redirect everything that's not a file or directory to index.php. 我希望它将不是文件或目录的所有内容重定向到index.php。 I am keeping another copy of the website in the subfolder may9-new (with the same .htaccess file copied into that directory). 我将网站的另一个副本保留在may9-new子文件夹中(将相同的.htaccess文件复制到该目录中)。 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. 但是问题是,我可以成功访问www.example.com/may9-new ,但是如果我访问www.example.com/may9-new/anything ,似乎我在根文件夹中看到该网站的版本。 。 Here's the .htaccess file: 这是.htaccess文件:

<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. 当涉及到.htaccess时,我几乎一无所知,所以请多多包涵。 I learned that RewriteCond is a condition for which RewriteRule should take place. 我了解到RewriteCond是应该发生RewriteRule的条件。 So I tried to write a condition that the path can't start with /may9-new . 所以我试图写一个条件,指出该路径不能以/may9-new开头。 This RewriteCond works only for www.example.com/may9-new and not if there are more slashes added. RewriteCond仅适用于www.example.com/may9-new ,如果添加了更多的斜杠,则无效。

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: 这是您可以在根目录以及子目录中保留的.htaccess:

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... 奇怪的是,此站点及其.htaccess文件在由...托管的其他域上工作。

Do you also have the same .htaccess file in your "may9-new" sub-folder? 您的“ may9-new”子文件夹中是否还具有相同的.htaccess文件?

I am keeping another copy of the website in the subfolder may9-new ( with the same .htaccess file copied into that directory ). 我将网站的另一个副本保留在may9-new子文件夹中( 将相同的.htaccess文件复制到该目录中 )。

If you also have, then there maybe a conflict, try to modify the .htaccess file of your sub-folder: 如果您也有,则可能有冲突,请尝试修改子文件夹的.htaccess文件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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