【发布时间】:2013-03-12 19:17:34
【问题描述】:
我不熟悉 .htaccess 文件,如果我的问题有点荒谬,请见谅。
当我要求时
localhost/mydomain/blog.php
我想要一个这种形式的网址:
localhost/mydomain/blog/
我的网站目录如下所示:
.htaccess 文件包括以下内容:
删除 .php 扩展名
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
</IfModule>
通过这个链接
<a href="blog">Blog</a>
网址是:
localhost/createforweb_1.1/blog
但我想添加一个斜杠,所以如果我在 htaccess 中添加这些行:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://localhost/createforweb_1.1/$1/ [L,R=301]
我收到此错误:
The requested URL /blog/.php was not found on this server.
我肯定很简单但是我对.htaccess rewrite有点经验!
最新的 .htaccess 文件(整个文件)
<IfModule mod_rewrite.c>
Options +FollowSymlinks
# Options +SymLinksIfOwnerMatch
RewriteEngine On
# RewriteBase /
</IfModule>
Options -MultiViews
## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]
替代解决方案
我刚刚为每个页面创建了一个文件夹,将文件移到其中并将它们重命名为 index.php。新的目录树如下所示:
现在我请求 blog 文件夹并且 blog/index.php 正在加载。 URL 变为 createforweb.gr/blog/
这是一个可接受的解决方案,还是会在未来产生问题?
【问题讨论】:
标签: .htaccess mod-rewrite url-rewriting