2

I am trying to beautify my URLs and want to get rid of the .php file extension. I've found a couple of other questions here such as this one (Removing the PHP file type extension) But none of the suggestions work for me. They send me to my 404.php or do not change the URL at all.

I figure the entire RewriteEngine code in my htaccess as a whole is conflicting in itself.

Here's what I got so far,

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
    RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


    RewriteBase /
    RewriteRule ^index\.php$ - [R]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /404.php [R]
</IfModule>

I would like my domains to look like

https://www.example.com/about

Fobos
  • 1,076
  • 8
  • 18
LOTUSMS
  • 10,317
  • 15
  • 71
  • 140

3 Answers3

1

Put in .htaccess

RewriteEngine on

RewriteBase /

RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]

#1)externally redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]

#2)Internally map "/file" back to "/file.php"
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,L]

ErrorDocument 404 ./404
RewriteCond %{REQUEST_URI} ^404/$
RewriteRule ^(.*)$ ./404 [L]

Redirect 301 /index /
Fobos
  • 1,076
  • 8
  • 18
  • Didn't work. I got sent to my 404 and a message that says it redirected too many times. Keep in mind, I have my htaccess setup to send pages not matching my REQUEST_FILENAME to my 404.php – LOTUSMS Jul 24 '18 at 17:57
  • I have updated the answer. Also try to restart the webserver – Fobos Jul 24 '18 at 18:02
  • 1
    Now we're unto something! Loving it so far! Except one minor detail. I had it so that the index.php showed a /...Now it shows example.com/index. How can I specifically omit the index from the main page? – LOTUSMS Jul 24 '18 at 18:17
  • 1
    I would be fine with either version `www.example.com/` or `www.example.com` – LOTUSMS Jul 24 '18 at 18:18
  • add RewriteRule ^(.*)$ /index.php/$1 [L] – Fobos Jul 24 '18 at 18:50
  • `The requested URL / was not found on this server.` On the home page, and 500 error on all other pages now – LOTUSMS Jul 24 '18 at 18:54
  • Oh man you're so close, yet so far lol No errors, except going to the home page send me to a 404 instead – LOTUSMS Jul 24 '18 at 19:07
  • I'll get back to this in an hour. – LOTUSMS Jul 24 '18 at 19:29
1

Don't mix Redirect, RedirectMatch and RewriteRule directives in same .htaccess as they come from different Apache modules and their load sequence is unpredictable.

Have it this way:

ErrorDocument 404 /404.php
RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Make sure to clear your browser cache before testing this change.

anubhava
  • 761,203
  • 64
  • 569
  • 643
0

remove .php from the link :

   RewriteEngine On
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^([^\.]+)$ $1.php [NC,L]
Mohammad
  • 1,549
  • 1
  • 15
  • 27