【问题标题】:htaccess question mark to slashhtaccess 问号斜线
【发布时间】:2021-04-19 15:40:27
【问题描述】:

我正在尝试使用 .htaccess 来做到这一点

https://xyzzz.com/country.php?id=157 >> https://xyzzz.com/country/57/

https://xyzzz.com/tld.php?id=123 >> https://xyzzz.com/tld/123/

我试试

RewriteRule ^country/([a-zA-Z0-9-.]+)$ country.php?id=$1

我的理解有限,我只是尽力而为。 有什么办法吗,或者我只是在做 htaccess 不可能做的事情……如果可能,请帮忙。

RewriteEngine On
RewriteBase /rectangle/

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domainsanalytics.com/$1 [R,L]

RewriteCond %{HTTP_HOST} ^www.domainsanalytics.com [NC]
RewriteRule ^(.*)$ https://domainsanalytics.com/$1 [L,R=301]

RewriteRule ^(country)/(\d+)/?$ $1.php?id=$2 [L]
RewriteRule ^(tld)/(\d+)/?$ $1.php?id=$2 [L]

这是我试图使 SEO 友好的 URL

https://domainsanalytics.com/country.php?id=219

【问题讨论】:

  • /rectangle 不是您的网址的一部分时,为什么您有RewriteBase /rectangle/

标签: apache .htaccess mod-rewrite url-rewriting query-string


【解决方案1】:

通用规则:您能否根据您展示的示例尝试以下、编写和测试。请在测试您的 URL 之前清除您的浏览器缓存。

RewriteEngine ON
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([\w-]+)/(\d+)/?$ $1.php?id=$2 [L]


仅适用于所示示例的特定规则:以下仅针对您所示示例非常具体,请确保您一次输入 Generic 或这些。

RewriteEngine ON
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(country)/(\d+)/?$ $1.php?id=$2 [L]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(tld)/(\d+)/?$ $1.php?id=$2 [L]

【讨论】:

  • 它确实有效,但它不会将 tlds.php?id=123 重定向到 tld/123/
  • @GraceJ.,所以您的意思是任何带有查询字符串?..... 的网址都应该被这条规则忽略?我在这里吗?请确认一次。
  • 不,它不动……如果我直接在浏览器中输入它就可以工作,那么只有domainsanalytics.com/country.php?id=37
  • @GraceJ.,你能告诉我你在哪里遇到问题,可以帮忙,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 2021-04-27
  • 2014-06-28
  • 2010-10-07
相关资源
最近更新 更多