php - Mod_Rewrite 和网站链接结构

标签 php .htaccess mod-rewrite seo

我有一个具有这种结构的网站

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/

相关文章:

php - codeigniter url 中的访问被拒绝错误具有 index.php

regex - 当单词具有相似的第一个字符时如何处理 RewriteRules?

.htaccess - .htaccess-RewriteRules工作正常,但浏览器地址栏显示完整(不友好)URL

php - Propel (PostgreSQL) 语法错误位于或接近 "."

php - Laravel 到基本 PHP 登录集成,工作!但无法设置 session

PHP SimpleXML 获取 innerXML

php - .htaccess 在开发、暂存和生产之间

mysql - .htaccess 允许来自 MySQL DB 的 IP

mod-rewrite - 内部子域到文件夹重定向

php - preg_match() vs strpos() 用于匹配查找?