douren5490 2017-08-21 12:01
浏览 50
已采纳

使用尾部斜杠PHP Htaccess删除索引,index.php错误句柄

I need some help with my Htaccess code.

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule (.*)\.php$ $1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L,QSA]

My Directory Structure is:

Public_Html

  css/style.css
   img/icon.png
   /2/user.php
  index.php
  create.php

Now the above htaccess code is removing the .php extension from the URL.

Examples

 -> example.com/index.php -> example.com/index
 -> example.com/2/user.php -> example.com/2/user

Problems with this Htaccess File

  1. I want to hide the index too Example: example.com/index -> example.com

  2. If user types example.com/index.php then he should be redirected to example.com

  3. If user type things which do not exist then it should point to example.com

    Example: example.com///*/// -> example.com

  4. When it removes the .php extension then a trailing slash should be added to the end.

Example: example.com/create.php -> example.com/create/

  1. If a user wants to access create.php and enter /create without / at the end then it should be automatically added.

    Example: example.com/create -> example.com/create/

In other words, if after removing .php these both works as same

**Example:** example.com/create = example.com/create/
  1. But user should still be able to access the directory

    Example: example.com/2 -> example.com/2/

    Here 2 is a folder.

I have some problem with this htaccess script. I searched a lot on Google and Stack Overflow but still can't find any solution.

*This is not a duplicate question. Please check it carefully before marking it as a duplicate. I would be glad if you help me.

  • 写回答

1条回答 默认 最新

  • doutuosai3504 2017-08-21 14:28
    关注

    Here is everything you need, all questions are answered with code, information, and examples! Providing 1 by 1 step for what each does and then the whole code!


    1-) Resolving .php extension

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

    example.com/user.php will be working if you go to example.com/user


    2-) Redirecting wrong links that don't exist to example.com

    If a user types a link which doesn't exist, for example, example.com///*, the user will be automatically redirected to example.com

    <IfModule mod_rewrite.c>
        Options -MultiViews
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php [QSA,L]
    </IfModule>
    

    3-) Redirect example.com/index.php or example.com/index to example.com

    RewriteCond %{THE_REQUEST} /index(\.php)?[/?\s] [NC]
    RewriteRule ^(.*?)index(?:\.php)?$ /$1 [L,R=301,NC,NE]
    

    4-) Remove trailing slash if it is not a directory else add

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [R=301,L]
    

    If you have a trailing slash added after a normal file, this will remove it. If it is a directory and has, it will remain. If it doesn't have, a trailing slash will be automatically added to it.

    You will be able to access directories!


    5-) Removing .php from URL ( Redirect to file name without .php )

    RewriteCond %{THE_REQUEST} \.(php|cfm)[\s?] [NC]
    RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
    RewriteRule ^(.+?)\.(cfm|php)$ /$1 [R=301,L,NE]
    

    The whole code together for .htaccess

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [R=301,L]
    
    RewriteCond %{THE_REQUEST} /index(\.php)?[/?\s] [NC]
    RewriteRule ^(.*?)index(?:\.php)?$ /$1 [L,R=301,NC,NE]
    
    RewriteCond %{THE_REQUEST} \.(php|cfm)[\s?] [NC]
    RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
    RewriteRule ^(.+?)\.(cfm|php)$ /$1 [R=301,L,NE]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php [L]
    
    <IfModule mod_rewrite.c>
        Options -MultiViews
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php [QSA,L]
    </IfModule>
    

    I hope this has helped you.

    Happy to help, enjoy!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。