使用 QUERY_STRING 时隐藏 URL 中的 index.php 和 .php

发布于 2024-10-09 01:19:39 字数 745 浏览 0 评论 0原文

我希望能够在使用 QUERY_STRING 时从我的 URL 中隐藏 index.php 和 .php。

现在,我成功地隐藏了index.php(访问某些目录时除外),在我的.htaccess中使用以下内容:

RewriteEngine On
RewriteCond $1 !^(codex|_core|admin|index\.php) [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]

我这样做是因为我有某些功能,例如登录/注销/注册,充当页面:示例.com/login

但是,我也有一些静态页面,这些静态页面不会从包含一堆 PHP 函数的文件中受益...例如 example.com/terms-and-conditions.php

本着统一的精神,如何将该 URL 转换为 example.com/terms-and-conditions 而不破坏 example.com/login?这可能吗?我尝试添加关于index.php删除重写的.php删除重写(显然从index.php中删除.php)无济于事。

我确实尝试使用位于此处的解决方案但它破坏了 example.com/login - 我认为是因为我使用函数/查询的方式。我需要在那里改变一些小东西吗?

I'd like to be able to hide both index.php and .php from my URLs while using QUERY_STRING.

Right now, I'm successfully hiding index.php (except when accessing certain directories) with the following in my .htaccess:

RewriteEngine On
RewriteCond $1 !^(codex|_core|admin|index\.php) [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]

I'm doing this because I have certain functions, such as login/logout/registration, acting as pages: example.com/login

However, I also have some static pages that wouldn't benefit from being inside one file with a bunch of PHP functions... such as example.com/terms-and-conditions.php

In the spirit of being uniform, how do I turn that URL into example.com/terms-and-conditions without breaking example.com/login? Is this even possible? I've tried adding the .php removal rewrite about the index.php removal rewrite (obviously removing the .php from index.php) to no avail.

I did try using the solution located here but it broke example.com/login — I assume because the way I'm using functions/queries. Is there just something small I need to change there?

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

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

发布评论

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

评论(1

独自←快乐 2024-10-16 01:19:39

想通了!只需要将两者结合起来,回想起来这似乎是显而易见的。 :)

RewriteEngine On

# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{HTTP_HOST} ^www\.domain\.com
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]

RewriteCond $1 !^(codex|_core|admin|index\.php) [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L] 

# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

Figured it out! Just needed to combine the two, which seems obvious in retrospect. :)

RewriteEngine On

# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{HTTP_HOST} ^www\.domain\.com
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]

RewriteCond $1 !^(codex|_core|admin|index\.php) [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L] 

# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

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