简体   繁体   中英

Remove directory name and php file extension

I have tried several ways to add two rules in my .htaccess file but I can't get this to work.

I want to remove the .php file extension but I also want to remove the directory /pages from appearing in the URL.

My directory structure is like this:

/
-/errors/
--/401.php

-/pages/
--/home.php
--/about.php
--/contact.php

-/includes/
--/header.php
--/footer.php

My .htaccess file is this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
ErrorDocument 404 /errors/404.php

What should I write into my .htaccess file to make it work??

I have tried the following but it doesn't seem to work:

RewriteRule ^pages(/.*|)$ $1 [L,NC]

In your site root .htaccess you can use this rule:

RewriteEngine On

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM