首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用.htaccess重写和重定向的麻烦

使用.htaccess重写和重定向的麻烦
EN

Stack Overflow用户
提问于 2019-03-30 22:24:40
回答 1查看 35关注 0票数 1

我试图为我的项目制作漂亮的/搜索引擎友好的URL,但我不能正确配置.htaccess文件。

我学会了如何从%domain%/profile.php重写和重定向到%domain%/profile,这样现有的书签就可以工作了,搜索引擎也不会有重复的页面。它在下面的代码中工作得很好:

代码语言:javascript
复制
RewriteCond %{ENV:REDIRECT_STATUS} 200 
RewriteRule .* - [L]

RewriteCond %{REQUEST_URI}  ^/profile\.php$
RewriteRule .* /profile [R=301,L]
RewriteRule ^profile$ /profile.php [L]

但是我不知道如何使用参数,比如从%domain%/profile.php?id=1%domain%/profile/1。下面是我尝试使用的代码:

代码语言:javascript
复制
RewriteCond %{QUERY_STRING}  ^id=([0-9]*)$ [NC]
RewriteRule ^profile\.php?id=([0-9]*)$ /profile/%1 [R=301,L]
RewriteRule ^profile/([0-9]*)$ /profile.php?id=%1 [L]

但是服务器返回的不是%domain%/profile/1,而是profile?id=1

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-31 19:34:49

谢谢,Nikos M.

此外,htaccess Tester可以提供很多帮助。

因为我们已经在%{QUERY_STRING}中检查了?id=0-9,所以只需在pattern中检查profile.php就足够了。另外,我在替换链接中添加了问号,所以我得到的是/profile/1而不是/profile/1?id=1

代码语言:javascript
复制
RewriteRule ^profile\.php?id=([0-9]*)$ /profile/%1 [R=301,L]

代码语言:javascript
复制
RewriteRule ^profile\.php$ /profile/%1? [R=301,L]

它现在运行得很好。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55432382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档