2010-11-13 91 views
0

是否有可能使一個文件夾中就像在下面的例子中的URL透明: example.com/test/ 改爲使用mod_rewrite example.com/mod_rewrite的改變URL路徑

我喜歡組織文件夾,但我想要一個漂亮乾淨的網址。

+0

改如何使用重定向或內部重寫?你能描述當用戶輸入一個URL時應該發生什麼,而另一個? – 2010-11-13 00:02:45

+0

你的意思是把'root_folder/test/page.html'這樣的文件作爲'example.com/page.html'嗎? – 2010-11-13 00:13:10

+0

@ Pekka-內部重寫將是最好的 @ Alberto-exactly – 2010-11-13 00:33:34

回答

2

可以實現與mod_rewrite的把這樣的事情在.htaccess文件的根文件夾:

RewriteEngine on 

# the first condition is for avoiding an infinite loop 
RewriteCond %{REQUEST_URI} !^/test/.* 
RewriteRule ^(.*)$ /test/$1 

這將重定向到/test/全部來自/請求的網頁/文件。

如果你只想要重定向的一些文件使用更具體的規則(S):

RewriteEngine on 

# the condition is for avoiding an infinite loop 
RewriteCond %{REQUEST_URI} !^/test/.* 
# redirecting only images files and files that start with "doc" 
RewriteRule ^(.*\.(jpg|png|gif))$ test/$1 [L] 
RewriteRule ^(doc.*)$ test/$1 [L] 

或者,如果你想使用不同的文件夾:

RewriteEngine on 

# the condition is for avoiding an infinite loop 
RewriteCond %{REQUEST_URI} !^/images/.* 
RewriteCond %{REQUEST_URI} !^/docs/.* 
# redirecting only images files and files that start with "doc" 
RewriteRule ^(.*\.(jpg|png|gif))$ images/$1 [L] 
RewriteRule ^(doc.*)$ docs/$1 [L]