htaccess url-rewriting无法使用配置设置

时间:2013-08-23 03:40:33

标签: php apache .htaccess url-rewriting

我正在运行XAMPP的win8机器上。我有一个使用.htaccess文件设置的虚拟主机目录。我一直在研究如何重写网址,我找到了RewriteEngine模块。在我的httpd.conf apache文件中,模块已经启用:

LoadModule rewrite_module modules/mod_rewrite.so

似乎下一步是更新我的.htaccess文件:

php_value register_globals off
DirectoryIndex default.php index.php
ErrorDocument 404 /filenotfound.html

RewriteEngine On
RewriteBase /
RewriteRule ^Home$ Default.php [L]
RewriteRule ^AboutMe$ About.php [L]
RewriteRule ^Work$ Work.php [L]
RewriteRule ^Blog//Categories$ Blog/Categories.php [L]
RewriteRule ^Blog//([^/.]+)/?$ Blog/Posts.php?val=$1 [L]

我已经关注了几个SO问题(configrewriting w/ params),但我无法让最简单的重写工作。我已经重启apache几次没有结果。

当我在这里时,这是我的文件夹结构归结为相关的一切:

root
.htaccess
Blog
   AuthorPanel.php
   Categories.php
   Post.php
   Posts.php
Default.php
About.php
Work.php

这些是我想要实现的改写(我已经尝试了大部分改写):

site.com/Default.php => site.com/Home
site.com/About.php => site.com/AboutMe

site.com/Blog/Categories.php => site.com/Blog/Categories
site.com/Blog/Posts.php?id=3&val=Android => site.com/Blog/Android
site.com//Blog/Post.php?id=4&title=Working+with+ActionBar => site.com/Blog/Working-with-ActionBar

更新1 在httpd-vhosts.conf中我甚至尝试过使用RewriteEngine并重写规则,但没有运气:

<VirtualHost *>
  DocumentRoot "C:/Users/ben/Documents/PHP/benWIT"
  ServerName benWIT.local
  <Directory "C:/Users/ben/Documents/PHP/benWIT">
    RewriteEngine on
    Order allow,deny
    AllowOverride all
    Allow from all
    Require all granted
    RewriteRule ^Home$ Default.php [L]
    RewriteRule ^AboutMe$ About.php [L]
    RewriteRule ^Work$ Work.php [L]
  </Directory>
</VirtualHost>

1 个答案:

答案 0 :(得分:1)

由于您使用的是htaccess,因此您需要确保在httpd.conf文件中将AllowOverride设置为All:

AllowOverride All

这将允许您使用htaccess文件。话虽如此,作为一般规则,你不想使用htaccess文件或启用AllowOverride,如果你有权访问apache配置文件只是因为它将使用更多的资源来搜索目录并找到htaccess文件等。放置更改进入httpd.conf文件或conf.d / example_host.conf要好得多。

另外一点,mod_rewrite过度使用,对于大多数用途来说真的有点过分。我建议你使用mod_alias(参见http://httpd.apache.org/docs/2.2/mod/mod_alias.html)。我应该指出这只能在服务器配置或虚拟主机中使用,因此它不能在htaccess文件中使用。但如果您可以在两者之间做出选择,那么应该优先考虑。

Alias /home /default.php
Alias /aboutme /about.php
Alias /work /work.php
AliasMatch /blog//([^/.]+)/? /blog/posts.php?val=$1

..等等。

使用mod_rewrite时,这是一个很好的阅读: http://httpd.apache.org/docs/2.2/rewrite/avoid.html