.htaccess - 删除扩展名后重写动态 url

标签 .htaccess mod-rewrite seo

我已经在我的 HTAccess 上工作了几天,但我遇到了死胡同。

我已经将文件重写并重定向为无扩展名,现在我需要重写 url 以使其对 SEO 友好。

以前,URL 是:http://www.example.com/member.php?playername=encodedName
我删除了扩展名:http://www.example.com/member?playername=encodedName
我不太明白:http://www.example.com/member/encodedName

这是我到目前为止得到的:

Code trying to redirect to SEO Friendly URL. Does NOT work.

 RewriteRule ^member/([^/]*)$ /member.php?playername=$1 [L] 
 RewriteRule ^squad/([^/]*)$ /squad.php?squadname=$1 [L] 
 RewriteRule ^article/([^/]*)$ /article.php?articlename=$1 [L]

Unless directory, remove trailing slash. Works.

 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)/$ /$1 [R=301,L]

Resolve .php file for extensionless php urls. Works.

 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME}\.php -f
 RewriteRule ^(.*)$ $1.php [L]

Redirect external .php requests to extensionless url. Works.

 RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
 RewriteRule ^(([^/]+/)*[^.]+)\.php $1 [R=301,L]

有人知道我做错了什么吗?这可能是一个非常新手的问题,我还没有遇到过。

最佳答案

尝试重定向到 SEO 友好 URL 的代码”部分看起来不错。当您将直接发出的请求重定向到 php 文件以删除扩展时,您并没有对您正在重写的 3 个 SEO 友好 URL 做任何具体的事情。

如果当您转到 http://www.example.com/member/encodedName 时,您没有收到 http://www.example 的内容。 com/member.php?playername=encodedName,那么这看起来像是一个多 View 问题。您需要在您的服务器/虚拟主机配置中关闭多 View ,或将其添加到 htaccess 文件的顶部(或在适当的现有 Options 语句中):

Options -Multiviews

为了执行重定向 SEO 友好的 URL,在 将外部 .php 请求重定向到无扩展名的 url。 "规则:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /member\.php\?playername=([^&\ ]+)([^\ ]*)
RewriteRule ^ /member/%1?%2 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /squad\.php\?squadname=([^&\ ]+)([^\ ]*)
RewriteRule ^ /squad/%1?%2 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /artcile\.php\?articlename=([^&\ ]+)([^\ ]*)
RewriteRule ^ /article/%1?%2 [L,R=301]

关于.htaccess - 删除扩展名后重写动态 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13369000/

相关文章:

php - 在 VideoJS 脚本上禁用视频下载

apache - 如何从 htaccess 文件访问自定义 header ?

php - 具有多个 PHP 变量和动态文本的 Mod_Rewrite

PHP URL 重写 - 没有获取查询字符串参数

php - 更改 WordPress 管理 URL

.htaccess - 将特定页面重定向到另一个域

php - Apache 的 mod_rewrite 和 %{REQUEST_URI} 问题

SEO - Mutli 国际域与一个站点?

php - 在使用重定向进行迁移时保持旧 URL 有效

分页的title标签和url,哪个好?