mod_rewrite 和漂亮的 url

发布于 2024-08-27 20:31:57 字数 1913 浏览 4 评论 0原文

我想要实现的目标:

1)http://localhost/en/script.php? param1=random 映射到 http://localhost/script.php? param1=random&language=English

  • 这必须始终有效。

2) http://localhost/en/random/text/here 将映射到 < a href="http://localhost/categories.php?term=random/text/here" rel="nofollow noreferrer">http://localhost/categories.php?term=random/text/here

  • 如果 random/text/here 是 404

我现在有什么:

RewriteEngine on
RewriteCond substr(%{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^en/(.+)$ categories.php?lang=English&terms=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ee/(.+)$ categories.php?lang=Estonian&terms=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^fi/(.+)$ categories.php?lang=Finnish&terms=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ru/(.+)$ categories.php?lang=Russian&terms=$1 [L]

RewriteRule ^en/(.*) $1?lang=English [QSA]
RewriteRule ^ee/(.*) $1?lang=Estonian [QSA]
RewriteRule ^ru/(.*) $1?lang=Russian [QSA]
RewriteRule ^fi/(.*) $1?lang=Finnish [QSA]

问题是什么:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 

它应该重定向到categories.php?lang=English IF /en/this/here/does/not/match/一个/脚本。如果我加载像 en/index.php 这样的 URL,它也会映射到categories.php?lang=English,因为 en/index.php 不存在。

我的想法:

substr(%{REQUEST_FILENAME},3) 会解决我的问题(因为目前 /ee/index.php 实际上映射到 /ee/index.php 而不仅仅是 /index.php)

不幸的是我不能'找到一种操作字符串的方法:/

What I'm trying to achieve:

1) http://localhost/en/script.php?param1=random is mapped to http://localhost/script.php?param1=random&language=English

  • This has to work always.

2) http://localhost/en/random/text/here will be mapped to http://localhost/categories.php?term=random/text/here

  • This has to work if random/text/here is 404

What I have at the moment:

RewriteEngine on
RewriteCond substr(%{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^en/(.+)$ categories.php?lang=English&terms=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ee/(.+)$ categories.php?lang=Estonian&terms=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^fi/(.+)$ categories.php?lang=Finnish&terms=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ru/(.+)$ categories.php?lang=Russian&terms=$1 [L]

RewriteRule ^en/(.*) $1?lang=English [QSA]
RewriteRule ^ee/(.*) $1?lang=Estonian [QSA]
RewriteRule ^ru/(.*) $1?lang=Russian [QSA]
RewriteRule ^fi/(.*) $1?lang=Finnish [QSA]

What is the problem:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 

It's supposed to redirect to categories.php?lang=English IF /en/this/here/does/not/match/a/script. If I load an URL like en/index.php it will also get mapped to categories.php?lang=English because en/index.php does not exist.

What I've thought:

substr(%{REQUEST_FILENAME},3) would fix my problem (as currently /ee/index.php is literally mapped to /ee/index.php instead of just /index.php)

Unfortunately I couldn't find a way to manipulate strings :/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

楠木可依 2024-09-03 20:31:57

我认为语言代码使 URL 映射到不存在的文件。切换这两个步骤,首先将语言代码移至查询字符串。这还有一个额外的优点,即将关键字步骤简化为单个 RewriteRule,因为它们不再需要同时执行两件事。

RewriteRule ^en/(.*) $1?lang=English [QSA,DPI]
RewriteRule ^ee/(.*) $1?lang=Estonian [QSA,DPI]
RewriteRule ^ru/(.*) $1?lang=Russian [QSA,DPI]
RewriteRule ^fi/(.*) $1?lang=Finnish [QSA,DPI]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ categories.php?terms=$1 [L,QSA]

I take it the language code is what makes the URL map to a non-existant file. Switch the two steps, moving the language code to the query string first. This also has the added advantage of simplifying the keyword step to a single RewriteRule, since they no longer need to do two things at once.

RewriteRule ^en/(.*) $1?lang=English [QSA,DPI]
RewriteRule ^ee/(.*) $1?lang=Estonian [QSA,DPI]
RewriteRule ^ru/(.*) $1?lang=Russian [QSA,DPI]
RewriteRule ^fi/(.*) $1?lang=Finnish [QSA,DPI]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ categories.php?terms=$1 [L,QSA]
傲鸠 2024-09-03 20:31:57

问题是您正在使用 L 标志。这意味着该规则将是最后执行的。

%{QUERY_STRING}

没有必要,添加 QSA,您将获得添加到 url 末尾的所有参数

尝试执行的操作:

RewriteRule ^en/(.*) $1?lang=English [QSA]

the issue is that you are using the L flag. Which means that rule will be the last to be executed.

also

%{QUERY_STRING}

isnt necessary, add QSA and you will get all parameters added to the end of the url

try to do:

RewriteRule ^en/(.*) $1?lang=English [QSA]
兲鉂ぱ嘚淚 2024-09-03 20:31:57

对于 substr 问题,您可以尝试绝对路径:

RewriteRule ^en/(.*) /$1?lang=English&%{QUERY_STRING}

或者

RewriteRule ^en/(.*) http:/localhost/$1?lang=English&%{QUERY_STRING}

另外,我可能会吹毛求疵,但如果您在 PHP 中基于语言代码进行语言评估,404'ed 非,不是会更容易吗? -现有的语言并使用

RewriteRule ^(.*?)/(.*) $2?lang=$1&%{QUERY_STRING}

编辑:取决于你有多少脚本,你不能做类似的事情:

RewriteRule ^(.*?)/(script.php|other.php) $2?lang =$1 [QSA]

= 有可以访问的管道列表文件吗?

For substr problem, you could try absolute paths:

RewriteRule ^en/(.*) /$1?lang=English&%{QUERY_STRING}

or

RewriteRule ^en/(.*) http:/localhost/$1?lang=English&%{QUERY_STRING}

Also, I might be nitpicking, but wouldn't it be easier if you did the language evaluation in the PHP base on language codes, 404'ed non-existent languages and used

RewriteRule ^(.*?)/(.*) $2?lang=$1&%{QUERY_STRING}

Edit: depending on how many scripts you have, can't you do something like:

RewriteRule ^(.*?)/(script.php|other.php) $2?lang=$1 [QSA]

= have pipe-list files, that are accesible?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文