开发者

.htaccess to add dynamic url segments to an existing rewrite rule

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 开发者_C百科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]


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:

RewriteEngine on

RewriteRule .* 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:

RewriteEngine on

RewriteRule !\.(css|js|png|jpg)$ index.php


To exclude all files that do exist, you could use the following code:

RewriteEngine on

RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule .* index.php


You can put rest of the path into third get parametar:

RewriteRule ^([^/]+)/([^/]+)/?(.*)$ account.php?type=$1&user=$2&rest_of_the_url=$3 [NC,L]

[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.

RewriteRule ^([^/]+)/([^/]+)/theme/([^/]+)/(.+\.(?:css|js|png|jpg))$ /library/themes/users/$3/$4 [NC,L]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜