.htaccess 将动态 url 段添加到现有重写规则
因此,我制定了这条规则
# Rewrite for account view
RewriteRule ^([^/]+)/([^/]+)/?$ account.php?type=$1&user=$2 [NC,L]
,将其转换为 http://www.site.com/user/s2xi
但现在我想对 URL 进行更多创意,并向其附加一个主题位置,以便:
http://www.site.com/user/s2xi/theme/slate/
这样我就可以访问主题的 CSS 文件,我只需要输入
http://www.site.com/user/s2xi/theme/slate/css/slate.css
如何修改或添加另一个 RewriteRule 即可获得我想要的结果?
另外,我如何能够使用 PHP 代码中的新 url 来应用和访问我的主题文件?
我的主题当前路径是 library/themes/users/slate
,其中包含子文件夹 /css
和 /js
我的尝试:
# Rewrite for user theme location
RewriteRule ^([^/]+)/([^/]+)/theme/([^/]+)/?$ account.php?type=$1&user=$2$theme=$3 [NC,L]
So I have this rule in place
# Rewrite for account view
RewriteRule ^([^/]+)/([^/]+)/?$ account.php?type=$1&user=$2 [NC,L]
which translate to http://www.site.com/user/s2xi
but now I want to get even more creative with the url and append a theme location to it so:
http://www.site.com/user/s2xi/theme/slate/
so that I can for instance access the CSS file for the theme I would simply need to type in
http://www.site.com/user/s2xi/theme/slate/css/slate.css
how can I modify or add another RewriteRule to be able to get the results I want?
also, how would I be able to apply and access my theme files with the new url in my PHP code?
my current path to my themes is library/themes/users/slate
with sub folders /css
and /js
My attempt:
# Rewrite for user theme location
RewriteRule ^([^/]+)/([^/]+)/theme/([^/]+)/?$ account.php?type=$1&user=$2$theme=$3 [NC,L]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议将所有请求重定向到一个文件,例如
index.php
,然后使用$_SERVER['REQUEST_URI']
获取 URL。然后,您可以使用该数据,例如将其分解。然后您将能够动态确定将加载哪个页面。您可以使用以下代码将所有请求重定向到
index.php
:您还可以排除特定文件类型,使其不被重定向到此 PHP 文件。例如 CSS、JavaScript 等静态内容。以下代码将排除所有带有 CSS、JS、PNG 或 JPG 扩展名的文件:
要排除所有存在的文件,您可以使用以下代码:
I suggest redirecting all requests to one file, e.g.
index.php
and then get the url by using$_SERVER['REQUEST_URI']
. You can then use this data and for example explode it. Then you will be able to dynamically determine which page will be loaded.You can use the following code to redirect all requests to
index.php
:You could also exclude particular filetypes from being redirected to this PHP file. For example static content like CSS, JavaScript etc. The following code will, for example, exclude all files with a CSS, JS, PNG or JPG extension:
To exclude all files that do exist, you could use the following code:
您可以将路径的其余部分放入第三个获取参数中:
[编辑]
好的,对于您的情况,我认为您应该编写 .htaccess 重定向,以便对于 css 和 js 文件,apache 直接访问它们。因为 php 处理会产生不必要的开销,除非你想检查用户凭据或类似的东西。
You can put rest of the path into third get parametar:
[EDIT]
Ok, for you case I think you should write .htaccess redirect so for css and js file apache goes directly to them. Because php processing would be unnecessary overhead, unless you want to check user credentials or something like that.