[英]Rewrite doesn't work?
I am trying to write a rewritrule in my htaccess file on my 000webhost
account but I cannot find a way to make it work! 我试图在我的
000webhost
帐户的htaccess文件中写一个可重写规则,但是我找不到使它起作用的方法!
all the files are uploaded to the root directory (public_html). 所有文件都上传到根目录(public_html)。 the
.htaccess
file is in the same directory as well. .htaccess
文件也位于同一目录中。
in the URL address bar i get something like this: domain.com/index.php?city=London
在URL地址栏中,我得到这样的信息:
domain.com/index.php?city=London
I need this to change to domain.com/city/London
我需要将其更改为
domain.com/city/London
But nothing seem to work via htaccess
file! 但是似乎无法通过
htaccess
文件工作!
here is my htaccess code: 这是我的htaccess代码:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^city/([^/]*)$ /index.php?city=$1 [L]
</IfModule>
any help would be great. 任何帮助都会很棒。
Your htaccess work properly. 您的htaccess正常工作。 In case, of user will visit the page
domain.com/index.php?city=LondonYou
you can redirect him. 如果用户将访问该页面
domain.com/index.php?city=LondonYou
您可以将其重定向。 By header location. 按标题位置。
header("Location: city/".$_GET['city']);
And now you need just know, when is user access by htacces and when not. 现在您只需要知道htacces何时访问用户,何时不访问。 Just use some trick.
只是使用一些技巧。
RewriteRule ^city/([^/]*)$ /index.php?city=$1&accessByHtaccess [L]
With condition. 有条件。
if(!$_GET['accessByHtaccess ']){
header("Location: city/".$_GET['city']);
}
To get proper 'nice urls', you'll need to redirect ugly url's to nice url's, and internally rewrite nice url's to the ugly, actually working, url's. 为了获得正确的“好的url”,您需要将丑陋的url重定向到漂亮的url,并在内部将漂亮的url重写为实际上工作的丑陋的url。 You already have the internal rewrite.
您已经具有内部重写。
From Apache 2.3.9 on, you can use the following solution. 从Apache 2.3.9开始,您可以使用以下解决方案。 The
END
-flag is not available in earlier versions and will throw an internal server error when used. 该
END
-flag是不是在早期版本,在使用时将抛出一个内部服务器错误。 Change the R
to R=301
to make the redirect permanent. 将
R
更改为R=301
以使重定向永久生效。 Only do this when all redirects and rewrites are working as you want them, as browsers will cache permanent redirects. 仅当所有重定向和重写均按您希望的方式工作时,才执行此操作,因为浏览器将缓存永久重定向。 The END-flag will stop rewriting completely, and will prevent an infinite loop.
END标志将完全停止重写,并防止无限循环。
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} city=([^&]*)
RewriteRule ^index\.php$ /city/%1? [R,L]
RewriteRule ^city/([^/]*)$ /index.php?city=$1 [END]
In earlier versions of Apache, you'll need to use a hack. 在早期版本的Apache中,您将需要使用hack。 This works, because
%{THE_REQUEST}
is only available like this with external requests, not when the url is rewritten: 之所以有效,是因为
%{THE_REQUEST}
仅在外部请求中可用,而不是在重写URL时:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/index\.php\?city=(.*)\s [NC]
RewriteRule ^ city/%1? [R,L]
RewriteRule ^city/([^/]*)$ /index.php?city=$1 [L]
Please note that I can't test either RewriteRule on this computer, but I am fairly sure I didn't make a typo. 请注意,我无法在这台计算机上测试任一RewriteRule,但我相当确定自己没有打错字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.