Quirinus
Quirinus

Reputation: 482

.htaccess matches files even without extension in URL

.htaccess

RewriteEngine On

# If directory, redirect to root
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ / [R,L]

# Don't show something.json
#RewriteCond %{REQUEST_FILENAME} -f
RewriteRule something\.json$ / [R,L]

# If not file or directory, go to the router
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /subfolder/router.php [L]

# Any other existing files get served normally

We can ignore the first two rules, they work. The problem is in the 3rd part/rule.

htaccess file is here:
https://subdomain.sitename.com/subfolder/.htaccess

I need any non-directory and non-file addresses to go to /subfolder/router.php.

I have a html file on the server:
https://subdomain.sitename.com/subfolder/sub2/filename.html

I want the URL:
https://subdomain.sitename.com/subfolder/sub2/filename
to map to /subfolder/router.php, since filename doesn't exist on the server (only filename.html exists).

But it serves/shows the filename.html file/page instead of mapping to /subfolder/router.php. Address bar still shows just filename without .html.

It works fine on my localhost, but not on the online server I uploaded the files to. I don't have access to the config files of the server.

I've read online, and looks like I'm on a virtual host, maybe?


I've tried:

Is there any way to force RewriteCond %{REQUEST_FILENAME} !-f to not match extensions of files on the server when they are not in the URL?

Or at least somehow fix the problem?

I'm losing my mind right now, after hours of trying things and reading online. Any help would be greatly appreciated.

Upvotes: 0

Views: 115

Answers (1)

Quirinus
Quirinus

Reputation: 482

Okay, after some thinking and trying things out, I solved it by changing:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /subfolder/router.php [L]

to

RewriteRule \.html$ /subfolder/router.php [L]

I'm not entirely happy, but it works for now, as the router.php file checks for file existence and uses a whitelist of files.

Upvotes: 1

Related Questions