doubi1797 2014-12-14 11:11
浏览 450
已采纳

隐藏php文件的扩展名并用htaccess隐藏index.php

This has been asked a thousand times, but not exactly how I want it. I have tried combining different solutions, but my .htaccess doesn't seem to do what it is supposed to.

# Not sure what this does?
Options +FollowSymLinks -MultiViews

# Turn mod_rewrite on
RewriteEngine On

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

# Redirect index in any directory to root of that directory
RewriteCond %{THE_REQUEST} ^[A-Z](3,9)\ /([^/]+/)*index\.[^\ ]*\ HTTP/
RewriteRule ^(([^/]+/)*)index(\.[a-z0-9]+)?$ http://www.example.com/$1? [R=301,L] 

Now my pages correctly change from domain.com/page1.php to domain.com/page1, however something goes wrong with domain.com/index.php.

I am testing this locally and when going to localhost/project everything works fine (the index.php opens, but you don't see that in the url) but when you explicitly navigate to localhost/project/index.php you are returned to the very root, i.e. localhost (which hen returns to http://localhost/xampp/splash.php). Of course, this is not what I want. I want localhost/project/index.php to return to `localhost/project/´.

An additional question, though: how do rewrite rules influence search engines. Will the pages (ie contact.php, about-us.php and so on) still be indexed?

Extra +1 and kudos for he or she who gives a detailed breakdown of what each line/rule in the htaccess in their answer does. I am still learning .htaccess, so every detail is important and relevant to me!

  • 写回答

1条回答 默认 最新

  • drfls28608 2014-12-14 11:15
    关注

    Lot of comments in this code in question seem to be mine :)

    Anyway you can use this code to fix your issue:

    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /project/
    
    # To externally redirect /dir/foo.php to /dir/foo
    RewriteCond %{THE_REQUEST} \s/+project/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
    RewriteRule ^ %1%2 [R=302,L,NE]
    
    # To internally forward /dir/foo to /dir/foo.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/project/$1\.php -f [NC]
    RewriteRule ^(.+?)/?$ $1.php [L]
    

    Update: For using in DocumentRoot:

    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    # To externally redirect /dir/foo.php to /dir/foo
    RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
    RewriteRule ^ %1%2 [R=302,L,NE]
    
    # To internally forward /dir/foo to /dir/foo.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
    RewriteRule ^(.+?)/?$ $1.php [L]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码