[英]Rewrite dynamic URL's and delete “.php” extension
I have a problem with my .htaccess file. 我的.htaccess文件有问题。 Well, I'm trying to do two things;
好吧,我正在尝试做两件事; Delete the extension ".php" of all the URL's, like: "localhost/about.php" to "localhost/about" or "localhost/about/".
删除所有URL的扩展名“ .php”,例如:将“ localhost / about.php”更改为“ localhost / about”或“ localhost / about /”。 And rewrite a dynamic url: "localhost/user/index.php?usr=username" to "localhost/user/username/" or "localhost/username".
并将动态网址:“ localhost / user / index.php?usr = username”重写为“ localhost / user / username /”或“ localhost / username”。
I have have found a method to do both things. 我已经找到一种可以同时做这两种事情的方法。 But if I have the code to delete the ".php" extension, the code to rewrite the dynamic url doesn't work.
但是,如果我有删除“ .php”扩展名的代码,则重写动态URL的代码将无法正常工作。 And if I don't have the code to delete the ".php" extension, the code to rewrite the dynamic url works.
如果我没有删除“ .php”扩展名的代码,则重写动态url的代码也可以工作。
That's the error that the website gives me when I try to open a profile page: 这是我尝试打开个人资料页面时网站给我的错误:
Not Found
未找到
The requested URL /user/enric was not found on this server.
在此服务器上找不到请求的URL / user / enric。
or 要么
Not Found
未找到
The requested URL /user/enric.php was not found on this server.
在此服务器上找不到请求的URL /user/enric.php。
What can I do? 我能做什么? Here is the code of the .htaccess file:
这是.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>
SOLUTION, BY JON LIN 解决方案, 林俊恩
<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>
You need to add a check that when you add the .php
extension to a request, that the php file actually exists, and you should move your user rewrite before your php extension stuff: 您需要添加一项检查,以确保在将
.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.