使用.htaccess文件来重定向没有连字符的名称。

huangapple go评论128阅读模式
英文:

How redirect name without hyphen by htaccess

问题

我遇到了从我的友好网址中删除连字符的问题,在我更新了Apache之后,似乎它停止工作了。

我的网址是
www.mydomain.com/sales/super-cumputer

需要获取"super-cumputer"而不包含"-"

//不想使用PHP来移除
$name_computer = $_GET['name_computer'];

需要将这个变成$1
//super computer

我的重定向目前是这样的

RewriteCond %{REQUEST_URI} !\-
RewriteRule ^sales/?([^/]*)?/?$ /my_products.php?name_computer=$1&new_page=1 [L,QSA]
英文:

I'm having problems removing the hyphen from my friendly url, after I updated apache it seems that it stopped working

my url would be
www.mydomain.com/sales/super-cumputer

need to get super-cumputer without "-"

//would not want to use PHP to remove
$name_computer = $_GET['name_computer'];

need this in $1
//super computer

my redirect is like this currently

RewriteCond %{REQUEST_URI} !\-
RewriteRule ^sales/?([^/]*)?/?$ /my_products.php?name_computer=$1&new_page=1 [L,QSA]

答案1

得分: 1

Firstly, your RewriteCondition is looking for URIs without a - in them, instead, you want it to look for one matching dashes.

然后,正如我在注释中提到的,您可以添加两个单独的组来匹配。

RewriteRule ^sales/?([^-/]*)\-?([^-/]*)?/?$ /my_products.php?name_computer=$1%20$2&new_page=1 [L,QSA]

个人而言,我不建议这样做,因为这是一个非常特定的匹配,如果您有一个变量,其中包含两个短划线以分隔单词(例如 /sales/new-super-computer/),这不会匹配。我更愿意在我的应用程序代码中处理这种类型的逻辑。

编辑:这个工具可以帮助您测试您的规则
https://htaccess.madewithlove.com?share=77ed5323-9db5-4a63-a9df-7864b4fbb147

英文:

Firstly, your RewriteCondition is looking for URIs without a - in them, instead, you want it to look for one matching dashes.

Then, as I mentioned in the comment, you can add two separate groups to match.

RewriteRule ^sales/?([^-/]*)\-?([^-/]*)?/?$ /my_products.php?name_computer=$1%20$2&new_page=1 [L,QSA]

I personally wouldn't recommend this though as this is a very specific match and if you have a variable with two dashes to separate words (ie. /sales/new-super-computer/, this would not match. I would rather handle this type of logic within my application code.

Edit: This tool can help you test your rules
https://htaccess.madewithlove.com?share=77ed5323-9db5-4a63-a9df-7864b4fbb147

huangapple
  • 本文由 发表于 2023年6月13日 08:11:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定