Home > Mobile >  Htaccess rewrite rule directory based on caracters of a word
Htaccess rewrite rule directory based on caracters of a word

Time:10-06

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???

CodePudding user response:

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.

  • Related