【问题标题】:htaccess url-rewriting not working with config setuphtaccess url-rewriting 不适用于配置设置
【发布时间】:2013-08-26 00:04:56
【问题描述】:

我在运行 XAMPP 的 win8 机器上。我有一个使用 .htaccess 文件设置的虚拟主机目录。我一直在研究如何重写 url,我发现了 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>

【问题讨论】:

    标签: php apache .htaccess url-rewriting


    【解决方案1】:

    由于您使用的是 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

    【讨论】:

    • 我将使用共享主机环境,所以我不确定我是否可以访问任何 apache 配置文件。现在我在 vhosts 中设置了一个虚拟主机,在我的 httpd.conf 中我有 将 AllowOverride All。我也没有在 Alias 链接中看到我设置这些别名的位置。感谢您的帮助
    • 您可以将别名链接放在 htaccess 文件中。由于您使用的是共享主机,因此无法解决,您必须使用 htaccess。只需确保 mod_alias 已加载。
    • 在将任何别名规则添加到 .htaccess 文件时,我还会收到服务器错误“500”。 LoadModule mod_alias.so 也在 httpd.conf 中加载。我还尝试在 vhost 和 httpd 文件中删除 RewriteEngine。
    • 我应该在 httpd 文件中有我的虚拟主机目录吗?查看 apache 错误日志时,我看到多个这样的错误:“site_directory/.htaccess: Alias not allowed here”
    • 是的,它应该在服务器配置或虚拟主机中。
    猜你喜欢
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    相关资源
    最近更新 更多