繁体   English   中英

重写动态URL并删除“ .php”扩展名

[英]Rewrite dynamic URL's and delete “.php” extension

我的.htaccess文件有问题。 好吧,我正在尝试做两件事; 删除所有URL的扩展名“ .php”,例如:将“ localhost / about.php”更改为“ localhost / about”或“ localhost / about /”。 并将动态网址:“ localhost / user / index.php?usr = username”重写为“ localhost / user / username /”或“ localhost / username”。

我已经找到一种可以同时做这两种事情的方法。 但是,如果我有删除“ .php”扩展名的代码,则重写动态URL的代码将无法正常工作。 如果我没有删除“ .php”扩展名的代码,则重写动态url的代码也可以工作。

这是我尝试打开个人资料页面时网站给我的错误:

未找到

在此服务器上找不到请求的URL / user / enric。

要么

未找到

在此服务器上找不到请求的URL /user/enric.php。

我能做什么? 这是.htaccess文件的代码:

<IfModule mod_rewrite.c>

Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^channels/page/([0-9]+)/?$ channels/index.php?p=$1 [L]
RewriteRule ^channels/([_0-9a-z-]+)/?$ channels/index.php?o=$1 [L]
RewriteRule ^channels/([_0-9a-z-]+)/([0-9]+)/?$ channels/index.php?o=$1&p=$2 [L]

# Delete ".php" extension and adds "/"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.php [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]

# User profiles
RewriteRule ^user/([^/]*)/$ /user/index/?usr=$1 [L,R=301]

</IfModule>

解决方案, 林俊恩

<IfModule mod_rewrite.c>

Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^channels/page/([0-9]+)/?$ channels/index.php?p=$1 [L]
RewriteRule ^channels/([_0-9a-z-]+)/?$ channels/index.php?o=$1 [L]
RewriteRule ^channels/([_0-9a-z-]+)/([0-9]+)/?$ channels/index.php?o=$1&p=$2 [L]

# User profiles
RewriteRule ^user/([^/]*)/$ /user/index.php?usr=$1 [L]

# Delete ".php" extension and adds "/"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule (.*)/$ $1.php [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]

</IfModule>

您需要添加一项检查,以确保在将.php扩展名添加到请求中时,该php文件确实存在,并且应该 php扩展名之前将用户重写移动:

<IfModule mod_rewrite.c>

Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^channels/page/([0-9]+)/?$ channels/index.php?p=$1 [L]
RewriteRule ^channels/([_0-9a-z-]+)/?$ channels/index.php?o=$1 [L]
RewriteRule ^channels/([_0-9a-z-]+)/([0-9]+)/?$ channels/index.php?o=$1&p=$2 [L]

# User profiles
RewriteRule ^user/([^/]*)/$ /user/index.php?usr=$1 [L]

# Delete ".php" extension and adds "/"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule (.*)/$ $1.php [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]

</IfModule>

您可以通过在线工具(例如http://www.generateit.net/mod-rewrite/index.php)生成htaccess规则

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM