重写不工作?重写、工作

2023-09-02 11:41:07 作者:少年多梦,不多心

我想在我的 000webhost的帐户我的htaccess文件写rewritrule但我不能找到一种方法,使其工作!

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!

中的所有文件上传到根目录下(的public_html)。在的.htaccess 文件在同一目录中。

all the files are uploaded to the root directory (public_html). the .htaccess file is in the same directory as well.

在URL地址栏我得到这样的: domain.com/index.php?city=London

in the URL address bar i get something like this: domain.com/index.php?city=London

我需要这个更改为 domain.com/city/London

但似乎没有通过 htaccess的工作文件!

这是我的htaccess code:

here is my htaccess code:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

RewriteRule ^city/([^/]*)$ /index.php?city=$1 [L]
</IfModule>

任何帮助将是巨大的。

any help would be great.

推荐答案

要得到适当的漂亮网址,您将需要重定向丑陋的URL到漂亮的网址,并在内部重新编写好的URL的丑,实际工作,网址。你已经有内部重写。

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. You already have the internal rewrite.

来自Apache 2.3.9,您可以使用下面的解决方案。该 END -flag是不是在早期版本可用,使用时将抛出一个内部服务器错误。更改研究 R = 301 使永久重定向。只有做到这一点时,所有的重定向和重写,你希望他们的工作,因为浏览器会缓存永久重定向。最终标志将完全停止重写,并将prevent一个无限循环。

From Apache 2.3.9 on, you can use the following solution. The END-flag is not available in earlier versions and will throw an internal server error when used. Change the R to R=301 to make the redirect permanent. 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.

RewriteEngine on
RewriteBase /

RewriteCond %{QUERY_STRING} city=([^&]*)
RewriteRule ^index.php$ /city/%1? [R,L]

RewriteRule ^city/([^/]*)$ /index.php?city=$1 [END]

在早期版本的Apache,你需要使用黑客。这工作,因为%{THE_REQUEST} 只能这样与外部请求,而不是当URL重写:

In earlier versions of Apache, you'll need to use a hack. This works, because %{THE_REQUEST} is only available like this with external requests, not when the url is rewritten:

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.

 
精彩推荐
图片推荐