简体   繁体   中英

Htaccess rewrite rule directory based on caracters of a word

how can i rewrite to deeper directory structure based on request_url?? eg:

%{REQUEST_URI} = /request_url.html

and rewrite to match file in folder /r/e/q/request_url.html

some help???

You could do something like the following at the top of your root .htaccess file using mod_rewrite:

RewriteEngine On

RewriteRule ^request_url\.html$ r/e/q/$0 [L]

The $0 backreference contains the entire URL-path that is matched by the RewriteRule pattern (saves repetition).


UPDATE: this is not for only one URL need the regex to match the 3 first characters for deeper directory

Ok, assuming you are only matching .html URLs for the document root only (ie. single path segments) - all .html URLs in the document root. Then you could do something like the following instead:

RewriteRule ^(.)([^/])([^/])[^/]*\.html$ $1/$2/$3/$0 [L]

Where $1 , $2 and $3 are backreferences to the 1st, 2nd and 3rd characters of the URL-path requested. ie. The 1st, 2nd and 3rd parenthesised subpatterns (capturing groups) in the RewriteRule pattern (regex).

The above matches /request_url.html and internally rewrites to /r/e/q/request_url.html . But it would fail to match /foo/request_url.html since it's not in the document root.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM