[英]How to redirect url to the url with htaccess
我不知道该如何质疑,但是我的问题是这样的
我已经为网址写了一条规则
RewriteRule ^mysite.com.pk/([a-zA-Z0-9]+) index.php?store=$1
RewriteRule ^mysite.com.pk/([a-zA-Z0-9]+)/products index.php?store=$1&view=products
RewriteRule ^mysite.com.pk/([a-zA-Z0-9]+)/products/([0-9]+) index.php?store=$1&view=products&category=$2
RewriteRule ^mysite.com.pk/([a-zA-Z0-9]+)/single/([0-9]+) index.php?store=$1&view=single&product=$2
我是htaccess
新手,所以我对此不太了解。 有没有一种方法,即使旧的网址如index.php?store=abc&view=single&product=123
也会被重定向到新的网址,如mysite.com.pk/abc/single/123
您必须对建议的规则进行一些小的修改才能使其生效。 而且,您还必须添加其他重写规则来实现您真正的要求:将“旧” URL重定向到新语法。
这可能是您要寻找的:
RewriteEngine on
RewriteRule ^/?([a-zA-Z0-9]+)$ index.php?store=$1 [END]
RewriteRule ^/?([a-zA-Z0-9]+)/products$ index.php?store=$1&view=products [END]
RewriteRule ^/?([a-zA-Z0-9]+)/products/([0-9]+)$ index.php?store=$1&view=products&category=$2 [END]
RewriteRule ^/?([a-zA-Z0-9]+)/single/([0-9]+)$ index.php?store=$1&view=single&product=$2 [END]
# index.php?store=xyz&view=single&product=123
RewriteCond %{QUERY_STRING} store=([^&]+)&?
RewriteCond %{QUERY_STRING} view=single&?
RewriteCond %{QUERY_STRING} product=([^&]+)&?
RewriteRule /?index.php$ /%1/single/%3 [END,R=301]
# index.php?store=xyz&view=products&product=123
RewriteCond %{QUERY_STRING} store=([^&]+)&?
RewriteCond %{QUERY_STRING} view=products&?
RewriteCond %{QUERY_STRING} category=([^&]+)&?
RewriteRule /?index.php$ /%1/products/%3 [END,R=301]
# index.php?store=xyz&view=products
RewriteCond %{QUERY_STRING} store=([^&]+)&?
RewriteCond %{QUERY_STRING} view=products&?
RewriteRule /?index.php$ /%1/products [END,R=301]
# index.php?store=xyz
RewriteCond %{QUERY_STRING} store=([^&]+)&?
RewriteRule /?index.php$ /%1 [END,R=301]
这些规则在.htaccess
样式文件和实际的http主机配置中应该相同。 请参阅以下说明。
如果您使用的是旧版本的apache http服务器,则可能必须用L
标志替换END
标志。 如果收到“内部服务器错误”(http状态为500),并且服务器抱怨错误日志文件中的END
标志,请尝试执行该操作。 在这种情况下,您可能必须添加一些其他条件来防止无限的重写循环。
请注意,我没有测试该规则集。 我希望它不会包含任何愚蠢的错误,您当然必须对其进行测试。
以上所有假设均假定mysite.com.pk
与您的主机名(“ domain”)匹配。 但这将不起作用,因为RewriteRules
在请求路径 (而不是完整的URL)上起作用。 如果要将规则的应用限制到特定主机,则可以添加前置条件以停止重写过程:
RewriteCond %{HTTP_HOST} !^mysite\.com\.pk`
RewriteRule .* - [END]
还有一个一般提示:您应该始终喜欢将此类规则放在http服务器主机配置中,而不是使用.htaccess
样式文件。 众所周知,这些文件容易出错,难以调试,并且确实降低了服务器的速度。 仅当您无法控制主机配置(阅读:真正便宜的托管服务提供商)或者您的应用程序依赖于编写自己的重写规则(这显然是安全噩梦)时,才提供它们作为最后的选择。 )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.