[英]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問題( config和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 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.