英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论