简体   繁体   English

.htaccess从URL中删除.php并添加斜杠

[英].htaccess Removing .php from URL and add trailing slash

I have a filesystem of PHP files and folder and want to make the URLs nicer but can't seem to find the right combo. 我有一个PHP文件和文件夹的文件系统,想要使URL更好,但似乎找不到正确的组合。 This works to make the admin.php work as just admin but I can't find a good way to add the trailing slash and redirect it to the pretty URL if someone hits the PHP URL. 这可以使admin.php像admin一样工作,但是如果有人点击PHP URL,我找不到一种添加尾部斜杠并将其重定向到漂亮URL的好方法。

#Remove .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L]

Wanted: 通缉:

domain/admin.php => domain/admin/
domain/admin     => domain/admin/

Virtual and Real subfolders too? 虚拟和真实子文件夹吗?

 domain/folder/   => loads index.php if exists otherwise loads folder.php in root

If anyone has any tips I'd appreciate it! 如果有人有任何提示,我将不胜感激!

Thanks! 谢谢!

You can solve all 3 tasks using these 2 rewrite rules in your site root .htaccess: 您可以在网站根目录.htaccess中使用以下2条重写规则来解决所有3个任务:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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