使用Htaccess删除文件夹名称(Remove folder name using Htaccess)

编程入门 行业动态 更新时间:2024-10-27 02:20:02
使用Htaccess删除文件夹名称(Remove folder name using Htaccess)

我认为我的htaccess代码存在一些问题,但我无法弄明白。

我需要的是。

1)从地址中删除.php扩展名,例如www.domain.com/index而不是www.domain.com/index.php

2)删除文件夹名称。 我的网站文件存储在名为public的文件夹中。 我想删除它。 www.domain.com/public/index.php至www.domain.com/index

我写了下面的代码,请告诉我这里有什么不对。

# This will hide the folder name public and the php text Options +FollowSymLinks -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME}.php -f RewriteCond %{REQUEST_URI} !/$ RewriteRule (.*) $1\.php [L] RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+public/([^\s]+) [NC] RewriteRule ^ %1 [R=301,L] RewriteCond %{REQUEST_FILENAME} !-f # if you face any problem than try to uncomment the below line and delete the line after it RewriteRule (?!^public/)^(.*)$ public/$1 [L,NC]

I think there is some problem in my htaccess code but I cannot able to figure out.

What I need is.

1) Remove .php extension from the address like www.domain.com/index instead of www.domain.com/index.php

2) Remove the folder name. My website files store in a folder called public. I want to remove it. www.domain.com/public/index.php to www.domain.com/index

I wrote the below code, please let me know whats wrong in this.

# This will hide the folder name public and the php text Options +FollowSymLinks -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME}.php -f RewriteCond %{REQUEST_URI} !/$ RewriteRule (.*) $1\.php [L] RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+public/([^\s]+) [NC] RewriteRule ^ %1 [R=301,L] RewriteCond %{REQUEST_FILENAME} !-f # if you face any problem than try to uncomment the below line and delete the line after it RewriteRule (?!^public/)^(.*)$ public/$1 [L,NC]

最满意答案

你可以这样做你的规则:

# This will hide the folder name public and the php text Options +FollowSymLinks -MultiViews RewriteEngine On RewriteBase / RewriteCond %{THE_REQUEST} \s/+public/(\S+) [NC] RewriteRule ^ %1 [R=301,L] RewriteCond %{REQUEST_FILENAME} !-f # if you face any problem than try to uncomment the below line and delete the line after it RewriteRule ^((?!public/).*)$ public/$1 [L,NC] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{DOCUMENT_ROOT}/public/$1\.php -f [NC] RewriteRule ^(.+?)/?$ $1.php [L]

You can have your rules like this:

# This will hide the folder name public and the php text Options +FollowSymLinks -MultiViews RewriteEngine On RewriteBase / RewriteCond %{THE_REQUEST} \s/+public/(\S+) [NC] RewriteRule ^ %1 [R=301,L] RewriteCond %{REQUEST_FILENAME} !-f # if you face any problem than try to uncomment the below line and delete the line after it RewriteRule ^((?!public/).*)$ public/$1 [L,NC] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{DOCUMENT_ROOT}/public/$1\.php -f [NC] RewriteRule ^(.+?)/?$ $1.php [L]

更多推荐

本文发布于:2023-08-07 06:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1461597.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹   名称   Htaccess   Remove   folder

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!