我有一个具有这种结构的网站
index.php/home.php/profile.php/news.php/photos.php
newsfeed.php - 用于新闻文章
photo.php - 用于照片
还有用户名可以让用户获取他的个人资料的短链接,例如
mysite.com/UserName
现在我在 .htaccess 中使用这段代码来重写用户名的 URL
RewriteEngine on
RewriteOptions MaxRedirects=1
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
RewriteRule ^([^/]+)/?$ profile.php?u=$1 [L]
现在我想像这样重写我所有的链接以使其对 SE 友好
index.php --> index
home.php --> home
photos.php --> photos
videos.php --> videos
newsfeed.php?id=xx --> news/xx
photo.php?id=xx --> photo/xx
有什么想法吗?
最佳答案
这是我要做的:
RewriteEngine on
RewriteOptions MaxRedirects=1
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
RewriteRule ^users/([^/]+)/?$ profile.php?u=$1 [L]
RewriteRule ^news/([0-9]+)/?$ newsfeed.php?id=$1 [L]
RewriteRule ^photo/([0-9]+)/?$ photo.php?id=$1 [L]
RewriteRule ^videos/?$ videos.php [L]
RewriteRule ^index/?$ index.php [L]
RewriteRule ^home/?$ home.php [L]
然后像这样添加它们。
关于php - Mod_Rewrite 和网站链接结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7949172/