Home > Software design >  hide .html .php ending over htaccess RewriteEngine
hide .html .php ending over htaccess RewriteEngine

Time:02-03

It actually worked because I only had .html files. However, it is now that I need the extension .php through a form.

I tried to copy the .htaccess rules, but the htaccess only takes the first rule.

My try:

RewriteEngine on
RewriteRule ^([^.?] )$ %{REQUEST_URI}.html [L]
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.html[? ].*$"
RewriteRule ^([^.?] )$ %{REQUEST_URI}.php [L]
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]

is there a way to hide .php as well as .html endings in the same rule, that it works?

CodePudding user response:

Try adding:

Options  MultiViews

instead of using mod_rewrite.

Or, you can try this:

RewriteEngine On

# return 404 if .html or .php is found at the end
RewriteRule \.(?:php|html)$ - [R=404,L]

# internally rewrite to .php if that exists
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ((?:^|\/)[^\.] )$ $1.php [END]

# internally rewrite to .html if that exists
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ((?:^|\/)[^\.] )$ $1.html [END]

Keep in mind that [END] stops all mod_rewrite rules from being processed after that rule, hence stops that recursion of rewriting the rewrited url to 404.

Or you can try:

RewriteEngine On

RewriteCond %{THE_REQUEST} .*\.(?:php|html)[\s\?]{1}
RewriteRule ^ - [R=404,L]

# internally rewrite to .php if that exists
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ((?:^|\/)[^\.] )$ $1.php [L]

# internally rewrite to .html if that exists
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ((?:^|\/)[^\.] )$ $1.html [L]

The second one will not stop all rewrite rules from being processed.

  •  Tags:  
  • Related