Mod重写- mod_rewrite和标题'.php'扩展


mod rewrite - mod_rewrite and titles with '.php' extension

我有一个数据库,有一个url列,如this-is-the-title.php我想在这里激活mod_rewrite:

Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+)$ news.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ news.php?url=$1

但是我有.php扩展的问题。此规则仅适用于this-is-the-title,而不适用于this-is-the-title.php。是否有任何方法来积极mod重写的标题有。php扩展名或我必须改变标题?

Thanks in advance

你的rewriterrule不允许点字符…如果你想让something.php匹配,使用这个:

^([a-zA-Z0-9-/.]+)$

我还简化了可能存在尾斜杠的事实:因为斜杠已经被允许了,所以尾斜杠也将匹配。

不要忘记添加一些rewritseconds来避免重写现有的目录或文件:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

顺便说一句,在重写的url中添加php扩展没有任何意义,你应该坚持使用没有扩展的干净url。

所以你会得到这样的结果:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([-/.a-zA-Z0-9]+)$ news.php?url=$1 [QSA,L]

可能使用类似于

的规则
RewriteRule ^([a-zA-Z0-9-/]+)'.php$ news.php?url=$1 [L]

?