繁体   English   中英

htaccess网址重写不适用于配置设置

[英]htaccess url-rewriting not working with config setup

我在运行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问题( configw / 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 on和rewrite规则,也没有运气:

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

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

AllowOverride All

这将允许您使用htaccess文件。 话虽如此,一般来说,如果您有权访问apache配置文件,则不想使用htaccess文件或启用AllowOverride,仅仅是因为它将使用更多资源来搜索目录并查找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

暂无
暂无

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

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