Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php .htaccess重写以重定向两个不同的文件夹_Php_.htaccess_Redirect_Mod Rewrite - Fatal编程技术网

Php .htaccess重写以重定向两个不同的文件夹

Php .htaccess重写以重定向两个不同的文件夹,php,.htaccess,redirect,mod-rewrite,Php,.htaccess,Redirect,Mod Rewrite,如果url点击重定向到两个导航 第一个条件:一些国家的产品信息已经建立 www.example.com/product-india => product-india/index.php www.example.com/product-us => product-us/index.php 第二个条件:其余国家将动态建设 www.example.com/product-japan => product/index.php www.example.com/product-aus

如果url点击重定向到两个导航

第一个条件:一些国家的产品信息已经建立

www.example.com/product-india  => product-india/index.php
www.example.com/product-us  => product-us/index.php
第二个条件:其余国家将动态建设

www.example.com/product-japan => product/index.php
www.example.com/product-australia => product/index.php
www.example.com/product-uk => product/index.php
其他国家也是如此

我所尝试的:

RewriteEngine on
RewriteRule ^/product-india /product-india/index.php [L]
RewriteRule ^/product-us /product-us/index.php [L]
RewriteRule ^/product-* /product/index.php?cat=$1 [L]
但页面仅满足此条件

RewriteRule ^/product-* /product/index.php?cat=$1 [L]
我的代码中有什么错误。
请帮助我并提前感谢

不清楚您到底想要实现什么,但我们可以提出两个建议

顺便说一下^/product-*不是您想要的正则表达式。我们必须假设您不知道正则表达式是如何构建的。我建议你开始读关于他们的书。当开发人员必须处理简单的文本分析任务时,它们是非常重要和强大的工具

以下是两条关于内部重写的建议—您自己根本没有实现任何外部重定向:

正如你在所写问题中所问的,一个简单的内部重写:

RewriteEngine on
RewriteRule ^/?product-india/?$ /product-india/index.php [END]
RewriteRule ^/?product-us/?$ /product-us/index.php [END]
RewriteRule ^/?product-\w*/?$ /product/index.php [END]
另一个将国家作为类别参数移交:

RewriteEngine on
RewriteRule ^/?product-india/?$ /product-india/index.php [END]
RewriteRule ^/?product-us/?$ /product-us/index.php [END]
RewriteRule ^/?product-(\w+)/?$ /product/index.php?cat=$1 [END]
如果您使用这些规则收到http状态500内部服务器错误,则很可能您操作的是非常旧版本的apache http服务器。在这种情况下,将[END]标志替换为旧的[L]标志,这很可能在这里同样适用,尽管这取决于您的具体设置

上述规则集在真正的http服务器主机配置和动态配置文件中也同样适用


还有一个一般性提示:您应该始终更喜欢将这些规则放在http服务器虚拟主机配置中,而不是使用动态配置文件.htaccess样式的文件。这些文件是出了名的容易出错,难以调试,而且它们确实降低了服务器的速度。只有在您无法控制主机配置的情况下(读:非常便宜的主机服务提供商,或者如果您的应用程序依赖于编写自己的重写规则,这显然是一个安全噩梦),才支持将它们作为最后一个选项。

您可以在站点根目录中使用规则。htaccess:

RewriteEngine On

# if <uri>/index.php exists then use it
RewriteCond %{DOCUMENT_ROOT}/$1/index..php -f
RewriteRule ^([\w-]+)/?$ $1/index.php [L]

# otherwise use product/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(product-\w+)/?$ product/index.php?cat=$1 [L,QSA"NC]

这将把/whappa buba重写为product/index.php?cat=buba,不是吗?是的。如果OP在开始时总是有word product,那么这个正则表达式可以进一步限制。我通常认为像/关于我们的页面很可能不会被重写。。。但你肯定是对的,这可以进一步完善…很高兴听到这个!希望你的项目是好的。玩得高兴