mod_rewrite 和漂亮的 url
我想要实现的目标:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为语言代码使 URL 映射到不存在的文件。切换这两个步骤,首先将语言代码移至查询字符串。这还有一个额外的优点,即将关键字步骤简化为单个 RewriteRule,因为它们不再需要同时执行两件事。
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.
问题是您正在使用 L 标志。这意味着该规则将是最后执行的。
也
没有必要,添加 QSA,您将获得添加到 url 末尾的所有参数
尝试执行的操作:
the issue is that you are using the L flag. Which means that rule will be the last to be executed.
also
isnt necessary, add QSA and you will get all parameters added to the end of the url
try to do:
对于 substr 问题,您可以尝试绝对路径:
或者
另外,我可能会吹毛求疵,但如果您在 PHP 中基于语言代码进行语言评估,404'ed 非,不是会更容易吗? -现有的语言并使用
编辑:取决于你有多少脚本,你不能做类似的事情:
RewriteRule ^(.*?)/(script.php|other.php) $2?lang =$1 [QSA]
= 有可以访问的管道列表文件吗?
For substr problem, you could try
absolute paths
:or
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
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?