2

I have tried a bunch of different ways now and am getting very frustrated

Options -Indexes
Options +MultiViews

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

^ Doesn't work at all

Options -Indexes
Options +MultiViews
<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /

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

# Redirect external .php requests to extensionless url
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://squeakcode.com/$1 [R=301,L]

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

</IfModule>

^Works but goes to a page that doesn't exist.

As mentioned the second code snippet works sort of. The problem is that it removes .php but doesn't pull the .php file so it goes to a not found page.

Am I missing something in my Apache config?

Squeakasaur
  • 245
  • 2
  • 13
  • Where is this .htaccess located and where are your `.php` files located? – anubhava Feb 05 '16 at 19:07
  • My .htaccess file is in my website folder under /var/www which is also where my .php files are. I am thinking I am missing something with the configuration. – Squeakasaur Feb 05 '16 at 19:10
  • This http://stackoverflow.com/questions/8371634/how-to-hide-php-extension-in-htaccess post may help you. The accepted answer looks to work. – Phil Feb 05 '16 at 19:11
  • Thanks Phil that is actually where I got the code that "sorta" works for me. – Squeakasaur Feb 05 '16 at 19:12
  • Did you check your apache error logs so you can see the path it's actually trying to access? That might help you see why it's getting 404. It will show you the path. Also you might try adding `/` to the rule. `/$1.php`. I've seen some servers try to use the full absolute path in the URL like (`http://domain.com/var/www/file.php`) which will give a 404. – Panama Jack Feb 05 '16 at 19:27
  • What is the URL you are entering in browser and what does it get redirected to? – anubhava Feb 05 '16 at 19:47
  • http://www.squeakcode.com/Music.php which changes to http://www.squeakcode.com/Music which is what I want but that goes to a page that doesnt exist – Squeakasaur Feb 05 '16 at 20:34
  • Turn off `MultiViews`. ie. `Options -MultiViews`. This is likely conflicting with your rewrite, since it's trying to do the same thing. (What is `([#?][^\ ]*)?` in the `RewriteCond` supposed to do? The fragment identifier isn't sent to the server.) – MrWhite Feb 05 '16 at 23:22
  • Ok I removed multiviews. The ([#?][^\ ]*)? was just copied over from someone elses code – Squeakasaur Feb 08 '16 at 14:41

0 Answers0