我试图为我的项目制作漂亮的/搜索引擎友好的URL,但我不能正确配置.htaccess文件。
我学会了如何从%domain%/profile.php重写和重定向到%domain%/profile,这样现有的书签就可以工作了,搜索引擎也不会有重复的页面。它在下面的代码中工作得很好:
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。下面是我尝试使用的代码:
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。
发布于 2019-03-31 19:34:49
谢谢,Nikos M.
此外,htaccess Tester可以提供很多帮助。
因为我们已经在%{QUERY_STRING}中检查了?id=0-9,所以只需在pattern中检查profile.php就足够了。另外,我在替换链接中添加了问号,所以我得到的是/profile/1而不是/profile/1?id=1。
RewriteRule ^profile\.php?id=([0-9]*)$ /profile/%1 [R=301,L]至
RewriteRule ^profile\.php$ /profile/%1? [R=301,L]它现在运行得很好。
https://stackoverflow.com/questions/55432382
复制相似问题