.htaccess重定向问题,带有多个.htaccess文件

发布于 2025-02-13 04:17:01 字数 847 浏览 0 评论 0原文

我在下面使用的.htaccess文件将www重定向到我的public_html目录上的non www,

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

它的工作正常和重定向如下,

    www.example.com
to
    https://example.com

在我的子目录中,称为最新的sub Directory,我有另一个.htaccess文件,

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]

如下

www.example.com/latest/index.php?page=1
to
www.example.com/latest/1

现在 即使我在主目录中都有.htaccess文件,该文件将www www www,我的这个目录也不会将www重定向到非www。

我想重定向

www.example.com/latest/1
to 
https://example.com/latest/1

我确定我的目录中的.htaccess文件会引起问题,但我不知道如何解决。让我知道这里是否有人可以帮助我。

谢谢!

I am using .htaccess file like below for redirect www to non www on my public_html directory

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

its working fine and redirect like below

    www.example.com
to
    https://example.com

Now in my sub directory called latest, I have another .htaccess file like below

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]

which is making my url clean like below

www.example.com/latest/index.php?page=1
to
www.example.com/latest/1

but even I have .htaccess file in home directory which is making www to non www, my this directory does not redirect www to non www.

I want redirect

www.example.com/latest/1
to 
https://example.com/latest/1

I am sure .htaccess file in my directory is causing issue but I do not know how to resolve it. Let me know if anyone here can help me for the same.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失退 2025-02-20 04:17:01

问题是,mod_rewrite没有继承(至少默认情况下)。如果您在“默认”目录中有一个重写规则。.您将必须在下面的所有后续目录中包含该规则。我从未见过(或听说过)允许mod_rewrite允许从父.htaccess文件继承的设置。通常,这只是被认为是“它的方式”。

更新

这个问题在服务器故障中也解释了这一点。 mod_rewrite从上一个htaccess文件中的规则甚至没有处理。

将2组结合在子目录中

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

# add this only to sub directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]

The problem is, mod_rewrite is not inherited (at least by default). If you have a rewrite rule in the "default" directory .. You're going to have to include that rule in all subsequent directories "below" that. I have never seen (or heard of) a setting for allowing mod_rewrite to allow inheritance from parent .htaccess files. In general, this is just accepted as "the way it is".

UPDATE

THIS QUESTION In Server Fault explains this as well. The mod_rewrite rules from the previous htaccess files don't even get processed.

COMBINE THE 2 in the SUB DIRECTORIES

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

# add this only to sub directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文