mod_rewrite - 友好的 URL 并删除页面扩展

发布于 2024-09-18 08:51:14 字数 1832 浏览 2 评论 0原文

我需要一些有关我尝试使用的 mod_rewrite 规则的帮助。我需要做两件事

  1. 更改动态 URL 以使其友好(例如 /content.php?CategoryID=1 更改为 /categories/1/)< /p>

  2. 删除 .php 我的页面上的扩展名(例如 /page2.php/page2

好的,我可以让数字 1 与我的 .htaccess 文件中的以下内容一起工作:

Options +Indexes
Options +FollowSymlinks

RewriteEngine on
RewriteOptions Inherit
RewriteBase /

RewriteRule ^category/([0-9]+)?$ category/$1/ [R]
RewriteRule ^category/([0-9]+)/?$ content.php?CategoryID=$1

我是然后尝试第二个,我遇到了一些问题。我正在使用以下内容:

RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.php$ /%1 [NC,R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(/.+)\.php$
RewriteRule ^(.+)\.php$ %1/ [R=301,L]

RewriteCond %{REQUEST_URI} ^(/.+)/$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^.+/$ %1.php [QSA,L]

上面重写了 URL,因此它看起来很友好(例如 /page2/),但它到达了我的 404 页面,我无法查看它,但我重写了 URL 的其他页面仍然有效(例如 /category/1/)。

这是我完整的 .htaccess 文件:

Options +Indexes
Options +FollowSymlinks

RewriteEngine on
RewriteOptions Inherit
RewriteBase /

RewriteRule ^category/([0-9]+)?$ category/$1/ [R]
RewriteRule ^category/([0-9]+)/?$ content.php?CategoryID=$1

RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.php$ /%1 [NC,R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(/.+)\.php$
RewriteRule ^(.+)\.php$ %1/ [R=301,L]

RewriteCond %{REQUEST_URI} ^(/.+)/$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^.+/$ %1.php [QSA,L]

ErrorDocument 404 /misc/404page.html

任何帮助将不胜感激,因为我对所有这些 mod_rewrite 和正则表达式都是新手。

I need a little help with some mod_rewrite rules I am trying to use. I need to do 2 things

  1. Change a dynamic URL so that it’s friendly (e.g. /content.php?CategoryID=1 change to /categories/1/)

  2. Remove the .php extension on my pages (e.g. /page2.php to /page2)

Ok so I can get number 1 to work by itself with the following in my .htaccess file:

Options +Indexes
Options +FollowSymlinks

RewriteEngine on
RewriteOptions Inherit
RewriteBase /

RewriteRule ^category/([0-9]+)?$ category/$1/ [R]
RewriteRule ^category/([0-9]+)/?$ content.php?CategoryID=$1

I am then trying number 2 and I'm running into a few problems. I am using the following:

RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.php$ /%1 [NC,R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(/.+)\.php$
RewriteRule ^(.+)\.php$ %1/ [R=301,L]

RewriteCond %{REQUEST_URI} ^(/.+)/$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^.+/$ %1.php [QSA,L]

The above rerwrites the URL so it looks friendly (e.g. /page2/) but it hits my 404 page and I can't view it but my other page where I rewrite the URL still works (e.g. /category/1/).

This is my full .htaccess file:

Options +Indexes
Options +FollowSymlinks

RewriteEngine on
RewriteOptions Inherit
RewriteBase /

RewriteRule ^category/([0-9]+)?$ category/$1/ [R]
RewriteRule ^category/([0-9]+)/?$ content.php?CategoryID=$1

RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.php$ /%1 [NC,R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(/.+)\.php$
RewriteRule ^(.+)\.php$ %1/ [R=301,L]

RewriteCond %{REQUEST_URI} ^(/.+)/$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^.+/$ %1.php [QSA,L]

ErrorDocument 404 /misc/404page.html

Any help would be greatly appreciated as I am new to all this mod_rewrite and regex stuff.

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

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

发布评论

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

评论(1

魔法少女 2024-09-25 08:51:14

我知道已经有一段时间了,我不是 100% 确定你想做什么,但我可能会考虑:

RewriteRule ^category/([0-9]+)$ category/$1/ [R,L]
RewriteRule ^category/([0-9]+)/$ content.php?CategoryID=$1 [L]
RewriteRule \.php? - [L]
RewriteRule ^(.+)\.php$ $1 [R,L]
RewriteRule ^([^/?])$ $1.php [L]

我需要更仔细地查看你的 .htaccess 和/或更好地了解您网站的结构以提供更好的信息。

I know it's been a while, and I'm not 100% sure what you were trying to do, but I would probably be looking at:

RewriteRule ^category/([0-9]+)$ category/$1/ [R,L]
RewriteRule ^category/([0-9]+)/$ content.php?CategoryID=$1 [L]
RewriteRule \.php? - [L]
RewriteRule ^(.+)\.php$ $1 [R,L]
RewriteRule ^([^/?])$ $1.php [L]

I'd need to look at your .htaccess more closely and/or have a better idea of the structure of your site to give better information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文