0

I am trying to hide .php in my URLS. it works with folowwing .htaccess in root files but not in subfolders. Like it works for https://example.com/demo but when i try to login or signup it dont work and gives 404. my login URL is below

https://example.com/demo/login.php ( then login.php page from root calls the login.php page in the inc folder)

Please guide

RewriteEngine On

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

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

# Resolve .php file for extensionless PHP URLs
RewriteRule ^([^/.]+)$ $1.php [L]
  • check: https://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess and https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/ – Akam Jan 30 '20 at 18:06
  • i have seen that but couldn't get the idea. If please fix the htaccess to work as i need, i will highly appreciate. – Syed Hassan Mujtaba Zaidi Jan 30 '20 at 18:08
  • We don't have similar environment you have to do it yourself! – Akam Jan 30 '20 at 18:13

1 Answers1

0

You should use RewriteBase then verify if it's a not a dir and simply remove the .php. Try this and let me know.

RewriteEngine On    
RewriteBase /

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

Attention GoDaddy users: In order to remove the extensions you need to enable MultiViews before. The code should look like this:

Options +MultiViews
RewriteEngine On

If you’re worried that search engines might index these pages as duplicate content, add a meta tag in the head of your HTML file

<link rel="canonical" href="https://alexcican.com/post/single-post" />
Patrick Simard
  • 2,294
  • 3
  • 24
  • 38
  • 1
    nice suggest! about godaddy (which is a horrible service) I use `Options +MultiViews` –  Jan 30 '20 at 18:50
  • the above didn't work for me. it is showing .php in my URL's. My server is Apache and hosted on digital ocean. I appreciate the help from you, but it is not working. i am seeing this https://example.com/demo/about.php – Syed Hassan Mujtaba Zaidi Jan 30 '20 at 19:18
  • You might need to add an htaccess in your sub directory or be more specific in your root htaccess for that particular directory, – Patrick Simard Jan 30 '20 at 19:22
  • Patrick, that means i have to place the .htaccess in subfolders also? i have placed in the root (public_html > script name > .htaccess and me login pages are here public_html > script name > inc > login, register pages, public_html > script name > include > headers, footers, navigations, config files – Syed Hassan Mujtaba Zaidi Jan 30 '20 at 19:25
  • Yes anywhere you have a php page that loads in the url (probably not include folder as that is usually included in an other php file) so only top level php files or you can be more specific in the root htaccess file so the "demo" folder has a set of rules. – Patrick Simard Jan 30 '20 at 19:31