2016-10-13 102 views
0

我为我的personnal网站(基本上是一个投资组合)制作了以下HTAccess文件。URL重写和PHP代码

# Error 
ErrorDocument 401 /error.php?id=401 
ErrorDocument 403 /error.php?id=403 
ErrorDocument 404 /error.php?id=404 
ErrorDocument 500 /error.php?id=500 

# Enable URL rewriting 
Options +FollowSymlinks 
RewriteEngine on 
RewriteOptions Inherit 

# Url rewrite for assets 
RewriteCond %{REQUEST_URI} !^/assets(/.*|)$ [NC] 
RewriteCond %{REQUEST_URI} !^/gallery(/.*|)$ [NC] 
RewriteRule ^.+$/[R=302,L] 

# Url rewrite for pages 
RewriteRule ^home$ index.php 
RewriteRule ^about$ about.php 
RewriteRule ^gallery/(.+)$ gallery.php?id=$1 
RewriteRule ^password/(.+)$ password.php?id=$1 
RewriteRule ^private/(.+)$ private_gallery.php?id=$1 
RewriteRule ^hidden/(.+)$ hidden_gallery.php?id=$1 
RewriteRule ^download/(.+)$ download.php?id=$1 
RewriteRule ^contact$ contact.php 

当我尝试直接通过我的网络浏览器访问这些URL时,这很好用。

在我的HTMLPHP代码中,我有这些旧的 URLS。我的意思是这些URL存在于我编写HTAccess文件之前。

<a href="private_gallery.php?id=SomeGalleryId">Open this gallery</a> 

当用户点击此网址后,他将被重定向到

/private_gallery.php?id=SomeGalleryId 

,而不是

/private/SomeGalleryId 

有没有办法改变这种状况不编辑我PHP/HTML代码?

回答

1

你可以使用另一个重写来匹配查询字符串,并做301重定向...它可能会导致SEO专家对视;最好是有形式/private/SomeGalleryId中的网址HTML代码和无形重定向到private_gallery.php?id=SomeGalleryId而不是解决这个方式,但...

RewriteCond %{REQUEST_URI} ^/private_gallery\.php$ 
RewriteCond %{QUERY_STRING} id=(\w+).* 
RewriteRule ^.*$ /private/%1 [R=301,L] 
+0

谢谢,如果它使搜索引擎优化工作得更好,我会编辑代码。 – Manitoba