2010-11-27 158 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" />