Sunday, 11 August 2019

.htaccess - rewriting /path.php to /path/ and not 404-ing files in sub-directories

Firstly, I apologise. This topic has been rehashed repeatedly, (I know. I read them.) But for whatever reason, my case is either odd, or I'm breaking something else in the .htaccess.

Objective

  1. Hide PHP file extensions,
  2. 301 any attempt at getting to them to their masquerading path,
  3. Allow PHP files within sub-directories to be actually referenced,
  4. Rewrite underscores in file names as slashes (/) so they appear to be nested. (edit: or translate / in the URL to be _'s in the file - unless the directory exists.)

... 3 & 4 are the kickers.

The following code is the .htaccess I've been using prior to this...

<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

## Imagine Some Code here ##

# Redirect to domain with www.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Same for HTTPS:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Ensure all directory URLs have a trailing slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
# Same for HTTPS:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
</IfModule>

I have tried adding the following snippets where I noted.

RewriteRule    ^$    /index.php    [L]
RewriteRule    ^([a-zA-Z0-9\-\_/]*)/$    /$1/index.php    [L]
RewriteRule    ^([a-zA-Z0-9\-\_/]*)\.(html|htm)$    /$1.php    [L]
RewriteRule    ^([a-zA-Z0-9\-\_/]*)$    /$1.php    [L]

and

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

and the most success was:

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ https://www.actual_domain.com.au/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ https://www.actual_domain.com.au/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

That last one was pretty good actually, however, I couldn't access php files at all in any format (with/without extension) in sub-directories. And all attempts at adding in something that handled requirement 4 just didn't work.

As much as I would love for someone to just go BAM, done. Use and enjoy. I would really like to understand what is going wrong too.

Thanks in advance.

-- edit --

As requested by misorude, the following URLs are examples.

URL: actual_domain.com.au/blog - Reads file: /blog.asp

URL: actual_domain.com.au/path/to-file - Reads file: /path_to-file.asp

URL: actual_domain.com.au/dir/file - Reads file: /dir/file.asp

With the last example (/dir/) the dir directory exists. With the /path/ one, it isn't a directory.

Now, as for my wording for objective 3, I have found that the .htaccess code that I have tried appears to prevent anything physically in a sub-directory returns 404. (case in point, that last example above referencing /dir/)

And lastly, with respect to the rewriting of / or _, the file contains the _ and I'd like it to present as a / in the url.



from .htaccess - rewriting /path.php to /path/ and not 404-ing files in sub-directories

No comments:

Post a Comment