2010-11-27 145 views
1

我有我的目錄設置像這樣:mod_rewrite的網址,搜索引擎優化

mywebsite/directory1/index.php 
mywebsite/directory2/index.php 
mywebsite/directory3/index.php 
mywebsite/directory4/index.php 

這不是搜索引擎友好。我該如何使用mod_rewrite做到以下幾點:

mywebsite/directory1/ 
mywebsite/directory2/ 
mywebsite/directory3/ 

我怎樣才能讓搜索引擎抓取我的網站,而無需多index.php's的問題是什麼?

你有什麼專業建議?

回答

1

將這個變成你的根.htaccess文件:

RewriteEngine On 
#if your index.php is located in the root folder: 
RewriteBase/
#or use that, if the path for your index.php is '/some/subfolder/index.php': 
#RewriteBase /some/subfolder/ 

#rewriting only if the request file doesn't exists (you don't want to rewrite request for images or other existing files) 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)? index.php?path=$1 [L] 

之後,你可以在你的根index.php文件使用REQUEST_URI:

var_dump($_SERVER['REQUEST_URI']); 

,或者你可以使用$ _GET [ 'path']:

var_dump($_GET['path']); 

在您的HTML和CSS中,對圖像和其他資源使用絕對路徑(或者更好地使用full URL的):

<img src="/image/image.jpg" alt="image" />