简体   繁体   English

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

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

I'm on a win8 machine running XAMPP. 我在运行XAMPP的win8机器上。 I have a virtual host directory set up with a .htaccess file. 我有一个使用.htaccess文件设置的虚拟主机目录。 I have been researching how to rewrite urls and I found the RewriteEngine module. 我一直在研究如何重写网址,并找到了RewriteEngine模块。 In my httpd.conf apache file, the module was already enabled: 在我的httpd.conf apache文件中,该模块已启用:

LoadModule rewrite_module modules/mod_rewrite.so

It seems like the next step was to update my .htaccess file like 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]

I have followed a couple SO questions ( config and rewriting w/ params ), but am unable to get even the easiest rewrites to work. 我遵循了几个SO问题( configw / params重写 ),但是即使是最简单的重写也无法正常工作。 I have restarted apache a couple of times with no results. 我已经重新启动了Apache几次,但没有结果。

While I'm here, this is my folder structure boiled down to everything relevant: 当我在这里时,这是我的文件夹结构,归结为所有相关内容:

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

And these are the rewrites I am looking to achieve (I have already tried most of them): 这些是我希望实现的重写(我已经尝试了大多数):

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

Update 1 In httpd-vhosts.conf I even tried using the RewriteEngine on and rewrite rules, with no luck either: 更新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>

Since you are using htaccess then you will need to make sure AllowOverride is set to All in your httpd.conf file: 由于您使用的是htaccess,因此您需要确保在httpd.conf文件中将AllowOverride设置为All:

AllowOverride All

This will allow you to use htaccess files. 这将允许您使用htaccess文件。 Having said that, as a general rule you don't want to use htaccess files or enable AllowOverride if you have access to the apache config files simply because it will use more resources to search the directory and find the htaccess files etc. Placing the changes into the httpd.conf file or conf.d/example_host.conf is much better. 话虽如此,一般来说,如果您有权访问apache配置文件,则不想使用htaccess文件或启用AllowOverride,仅仅是因为它将使用更多资源来搜索目录并查找htaccess文件等。放置更改放入httpd.conf文件或conf.d / example_host.conf会更好。

One other note, mod_rewrite is over used and is really overkill for most purposes. 另一个要注意的是,mod_rewrite被过度使用,对于大多数用途来说实际上是多余的。 I would advice you use mod_alias (see http://httpd.apache.org/docs/2.2/mod/mod_alias.html ) instead. 我建议您使用mod_alias(请参阅http://httpd.apache.org/docs/2.2/mod/mod_alias.html )代替。 I should point out this can only be use in server configs or virtual hosts, so it will not work in a htaccess file. 我应该指出,这只能在服务器配置或虚拟主机中使用,因此不能在htaccess文件中使用。 But it should be given preference should you have the choice between the two. 但是,如果您在两者之间进行选择,则应优先考虑。

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

.. and so on. .. 等等。

Here is a good read on when not to use mod_rewrite: http://httpd.apache.org/docs/2.2/rewrite/avoid.html 这是什么时候使用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