0

When I enable MultiViews, if I visit bad URLs, the my page (index.php) is still reached, when I want the user to get a 404 error instead. I'm trying to figure out how to fix this without creating rules in my .htaccess.

For example, "www.mydomain.com/index/blah/blah", visits index.php, but I want it to fail due to the extraneous trailing garbage URL components. Similarly for "/contact/blah/awuihda/hiu", which shows the content of contact.php, should give a 404 error, because "/blah/awuihda/hiu" doesn't exist.

If I disable MultiViews it works fine, but then I can't abbreviate the URL as much as I want to (for example, can't type "/contact" to bring up "contact.php").

clearlight
  • 12,255
  • 11
  • 57
  • 75
p 8
  • 33
  • 1
  • 6

2 Answers2

0

You could just use the following so the .php extension is not required, which is the usual approach:

RewriteEngine on
# Remove .php if it's present with a 301 redirect
RewriteRule ^(.+)\.php$ $1 [R=301,L]
# If a request doesn't exist as a directory, file or symlink
# and it does exist with .php appended, rewrite to that
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L]

I know it's adding a rule to .htaccess but it's a one off that works for everything and also means you're not hitting potential duplicate content allowing the same thing to be served with or without .php (or indeed with anything at all trailing after it as in your examples). Hope it's useful.

It could go in main server config but would need altering.

  • How would i need to change this to add it into my server configuration? – p 8 Jan 04 '17 at 04:42
  • With this in my server configuration it redirects my **/news** site so **/games/csgo/news**, which returns a 404. – p 8 Jan 04 '17 at 04:50
  • I updated it to work in the server configuration instead of .htaccess –  Jan 04 '17 at 04:53
  • Let me know if you still get a problem with your news site, not sure exactly what you're saying it was doing, redirecting /news to /games/csgo/news ? –  Jan 04 '17 at 04:54
  • Sorry one more thing, in your server config, it needs to go within the block for your document root or the file tests won't work. –  Jan 04 '17 at 04:56
  • Ignore that last comment. Was a bug from Chrome i think. Thanks but now i get the message **The page isn't redirecting properly**. Here is a Link to my config [link](http://pastebin.com/Wa50PmjN) EDIT: It is unter my Directory block. – p 8 Jan 04 '17 at 05:02
  • I added the `[L]` flag to the bottom rule which should fix that. If not, change it to `[END]`. Let me know how it goes. –  Jan 04 '17 at 05:31
  • Thank you very much but i think i found a way to fix my problem. – p 8 Jan 04 '17 at 05:39
0

I found a solution which works for me.

Options -Indexes SymLinksIfOwnerMatch -MultiViews

RewriteEngine On

RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]

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

Source: link

Community
  • 1
  • 1
p 8
  • 33
  • 1
  • 6