regex - .htaccess, rewriting of filename with same name as directory -
given file structure follows:
|- .htaccess (1) |- portfolio.html |- portfolio(dir) -| |- .htaccess (2)
i rewrite link /portfolio /portfolio.html. problem there exists directory named portfolio.
this question similar 1 answered here:
.htaccess, proper rewriting of directory , file same name
unfortunately doesn't quite solve problem.
here proposed solutions accepted answer above link:
solution 1: manually re-route requests portfolio directory remove slash.
# manually re-route portfolio/ requests portfolio rewritecond %{request_filename} portfolio/$ rewriterule ^(.*)/$ $1 # hide extension rewritecond %{request_filename}\.html -f rewriterule ^(.*)$ $1.html
the downside having hard-code "portfolio" edge case directly rewrite rules, , still result in browser being first redirected portfolio/
solution 2: set directoryslash off , remove directory exists test
# disable automatic directory detection directoryslash off # hide extension rewritecond %{request_filename}\.html -f rewriterule ^(.*)$ $1.html
setting directoryslash off fix issue best, may break other parts of site want auto directoryslash.
and indeed answer above linked stack overflow answer solve problem if there no .htaccess rule in directory same name filename, i.e. rewriterule in .htacess (2) file shown above stops working.
can suggest way of achieving this? thanks
this rule should work htaccess (2)
:
rewriteengine on rewritecond %{request_uri}::$1 ^(.*?)/(.*)::\2$ rewriterule ^(.*)$ - [e=base:%1] # if .html exists use rewritecond %{document_root}%{env:base}\.html -f [nc] rewriterule ^$ /%{env:base}.html [l]
Comments
Post a Comment