Php 使用.htaccess(Apache)重写多语言站点的URL
如果我已经有一个虚拟子目录,我就不知道如何在.htaccess中剪切扩展名(.php) 1) localhost/admin/index.php?lang=en->localhost/admin/en/Php 使用.htaccess(Apache)重写多语言站点的URL,php,.htaccess,url,multilingual,Php,.htaccess,Url,Multilingual,如果我已经有一个虚拟子目录,我就不知道如何在.htaccess中剪切扩展名(.php) 1) localhost/admin/index.php?lang=en->localhost/admin/en/ 2) localhost/admin/index.php?lang=ru->localhost/admin/ru/ 对于案例1)和案例2),我已经写过了,而且很有效。 用->RewriteRule^([a-z]+)(/)?$index.php?lang=$1[QSA,L] 请考虑(E&RU是虚拟
2) localhost/admin/index.php?lang=ru->localhost/admin/ru/ 对于案例1)和案例2),我已经写过了,而且很有效。
用->
RewriteRule^([a-z]+)(/)?$index.php?lang=$1[QSA,L]
请考虑(E&RU是虚拟目录) 3) localhost/admin/index.php?lang=en/createprofiles.php->localhost/admin/en/createprofiles.php
4) localhost/admin/index.php?lang=ru/createprofiles.php->localhost/admin/ru/createprofiles.php
对于案例3)和案例4),我也写了,而且很有效。
RewriteRule^([en|ru]{2})/(.*)$$2?lang=$1&%{QUERY_STRING}[L,QSA]
但是我想剪切.php扩展名。
最后一个链接必须类似于:localhost/admin/en/createprofiles/
你能帮我吗:)。我什么都试过了(大约8个小时),但都没用
谢谢大家! 这将是一个实现两个方向的示例:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)lang=(en|ru)(?:&|$)
RewriteRule ^/?admin/?$ /admin/%1 [QSA,R=301]
RewriteCond %{QUERY_STRING} (?:^|&)lang=(en|ru)(?:&|$)
RewriteRule ^/?admin/index\.php$ /admin/%1 [QSA,R=301]
RewriteCond %{QUERY_STRING} (?:^|&)lang=(en|ru)(?:&|$)
RewriteRule ^/?admin/createprofiles\.php$ /admin/%1/createprofiles [QSA,R=301]
RewriteRule ^/?admin/(en|ru)/?$ /admin/index.php?lang=$1 [QSA,END]
RewriteRule ^/?admin/(en|ru)/createprofiles/?$ /admin/createprofiles.php?lang=$1 [QSA,END]
一开始使用302临时重定向(而不是R=301
)是一个好主意,只有在确定所有设置都正确后,才可以将其更改为301永久重定向。这样可以防止在尝试时出现缓存问题
如果您使用上述规则收到一个内部服务器错误(http状态500),那么很可能您操作的是非常旧版本的apache http服务器。在这种情况下,您将在http服务器错误日志文件中看到不支持的[END]
标志的明确提示。您可以尝试升级或使用较旧的[L]
标志,在这种情况下可能也会使用相同的标志,不过这取决于您的设置
此实现将在http服务器主机配置中或在动态配置文件(“.htaccess”文件)中同样工作。显然,重写模块需要在http服务器中加载,并在http主机中启用。在使用动态配置文件的情况下,您需要注意在主机配置中启用该文件的解释,并且该文件位于主机的DOCUMENT\u ROOT
文件夹中
还有一个一般性的注释:您应该总是更喜欢将这些规则放在http服务器主机配置中,而不是使用动态配置文件(“.htaccess”)。这些动态配置文件增加了复杂性,通常是导致意外行为的原因,难以调试,而且它们确实降低了http服务器的速度。只有在您无法访问真正的http服务器主机配置(读:真正廉价的服务提供商)或坚持编写自己的规则的应用程序(这显然是一个安全噩梦)的情况下,才提供它们作为最后一个选项 这个奇怪的URL来自哪里:
/admin/index.php?lang=en/createprofiles.php
?虽然从技术角度来看这当然是可能的,但它违反了所有标准和实践。应该是/admin/createprofiles.php?lang=en
。或者是/admin/index.php?lang=en&action=createprofiles
…你的意思是,如果url是/admin/createprofiles.php?lang=en
我可以使用重写规则并将其显示为/admin/en/createprofiles/
?是、否和是:您当然可以实现一个规则,将/admin/en/createprofiles
内部重写为/admin/createprofiles.php?lang=en
,为什么不可以?不:你不能实施一个规则,以某种方式神奇地改变你所提供的参考资料。但是是的,如果需要,您当然可以实现第二条规则,将/admin/createprofiles.php?lang=en
外部重定向到/admin/en/createprofiles
。请您为1)/admin/index.php?lang=en->/admin/en/
2)/admin/createprofiles.php?lang=en->编写一个正确的.htaccess规则示例/admin/en/createprofiles/
请注意:en也可以是ru(我的意思是语言选择器应该是for(en | ru))当然,我可以给你一个例子……谢谢Arkascha!我会根据你的例子和解释来做。