使用.htaccess清除URL或不使用斜杠

时间:2014-01-08 13:32:32

标签: php apache .htaccess mod-rewrite trailing-slash

昨天我发布了一个关于.htaccess文件的问题。现在正在运作,但我有另一个问题。例如:

当我尝试去“localhost / about”时,它不起作用,它给我一个“错误404”:

  

未找到

     

找不到请求的URL / Applications / MAMP / htdocs / user / enric /   在这台服务器上。

但是当我去“localhost / about /”时它会起作用。为什么呢?

我希望两个网址都能正常运行。有人能帮助我吗?

这是我的.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]

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

# Check to see if the URL points to a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Trailing slash check
RewriteCond %{REQUEST_URI} !(.*)/$

# Add slash if missing & redirect
RewriteRule ^(.*)$ $1/ [L,R=301]

# 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>

谢谢!

1 个答案:

答案 0 :(得分:2)

DocumentRoot/.htaccess

中尝试这个稍微修改过的代码
Options +FollowSymLinks -Multiviews

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]

# Check to see if the URL points to a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# Trailing slash check
RewriteCond %{REQUEST_URI} !(.*)/$
# Add slash if missing & redirect
RewriteRule ^(.*)$ $1/ [L,R=301]

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

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

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]