2013-08-23 13 views
2

我在運行XAMPP的win8機器上。我有一個用.htaccess文件設置的虛擬主機目錄。我一直在研究如何重寫網址,並找到了RewriteEngine模塊。在我的httpd.conf文件的Apache,已經啓用的模塊:htaccess url-rewriting不能使用配置設置

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

由於您使用htaccess的,那麼你將需要確保的AllowOverride設置爲所有在httpd.conf文件:

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

+0

我將使用一個共享的託管環境,所以我不知道如果我將有機會獲得任何Apache的配置文件。現在我有一個虛擬主機設置在虛擬主機,並在我的httpd.conf我有將AllowOverride全部。我沒有在Alias鏈接中看到我設置了這些別名。感謝您的幫助 – benbob

+0

,您可以將別名鏈接放在htaccess文件中。由於您使用的是共享主機,因此無法解決問題,因此您必須使用htaccess。只要確保加載了mod_alias即可。 – Rijndael

+0

將任何別名規則添加到.htaccess文件時,我也會收到服務器錯誤「500」。 LoadModule mod_alias.so也加載到httpd.conf中。我也嘗試在vhost和httpd文件中刪除RewriteEngine。 – benbob