【问题标题】:.htaccess file only inside a sub folder and not the root folder.htaccess 文件仅在子文件夹内而不是根文件夹内
【发布时间】:2017-06-10 08:11:12
【问题描述】:

我有一个正在更新的网站。我已将所有库、资源、配置等复制到文件夹 newsite。由于我没有实现任何 url 重写根旧站点,我想在更新站点中实现它。因此,我在 newsite 文件夹中添加了 .htaccess 文件。下面是文件夹结构。

root/
|-- config/
|-- libraries/
|-- resources/
|-- newsite/
|     |-- config/
|     |-- libraries/
|     |-- resources/
|     |-- index.php
|     |-- .htaccess
|-- index.php
|-- login.php
|-- services.php
|-- contact.php

我希望旧站点以现在的方式运行,但我想实现对新站点的 url 重写。

基本上,新网站的网址是:

https://exmple.com/newsite/
https://exmple.com/newsite/services
https://exmple.com/newsite/login
https://exmple.com/newsite/contact

或任何其他网址。下面是我写的重写规则。

RewriteEngine On
RewriteBase /newsite
RewriteCond %{REQUEST_URI} ^/newsite
RewriteRule ^newsite/(.+)$ /newsite/index.php?page_name=$1 [QSA,L]

【问题讨论】:

  • /newsite/ 对于 root/newsite/.htaccess 被丢弃,所以这个规则只对 root/.htaccess 有意义。同时删除多余的RewriteBase

标签: php apache .htaccess mod-rewrite


【解决方案1】:

如果我理解正确,以下应该可以工作

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,R=301]

这会将所有不以 /public/ 或 /root/ 开头的请求重定向到以 /public/ 开头的 URL。

希望对您有所帮助。

【讨论】:

  • 这不起作用。在您的情况下,您忽略了该文件夹。但是如果文件夹在 url 中,那么它应该重写 url。
  • 创建一个路由页面,添加后所有的url都是路由。根文件夹中的 htaccess 文件,例如 `root/.htaccess`
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-19
  • 1970-01-01
  • 2014-06-02
  • 1970-01-01
  • 2015-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多