doushi8187 2013-10-02 04:14
浏览 40
已采纳

虚荣URL和文件扩展脚本干扰在一起

I have the following code in my htaccess file:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

#vanity URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_]*)$ /profile.php?username=$1 [L,QSA]
RewriteCond %{THE_REQUEST} \ /profile\.php\?username=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /%1?%2 [L,R=301]

#remove file extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]


ErrorDocument 400 /404.php
ErrorDocument 401 /404.php
ErrorDocument 403 /404.php
ErrorDocument 404 /404.php 
ErrorDocument 500 /404.php

<Files .htaccess>
order allow,deny
deny from all
</Files>

Regarding the vanity URLs, it works fine, but when it comes to remove the php extensions at the end of the URLs, it doesn't work:

1. if i enter mysite.com/home.php, i do get redirected to home.php but the extension is still there in the address bar.

2. if i enter mysite.com/home, i get redirected to my custom error page because home is not a username is my database.

How can I fix 1. and 2. ?

  • 写回答

1条回答 默认 最新

  • dongzouche9108 2013-10-02 22:01
    关注

    You need to do the php extension check before the username routing. This, of course, will break if you happen to have a php file that is the same as a username, no way around this unless you prefix all of your usernames.

    So swap your rules around and add another rule for redirecting direct accesses to php files:

    # we don't want to mess with /profile.php access because there's a rule later that deals with that
    RewriteCond %{REQUEST_URI} !^/profile\.php
    RewriteCond %{THE_REQUEST} \ /(.*)\.php(\?|\ |$)
    RewriteRule ^ /%1 [L,R=301]
    
    # add file extensions back to URI
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    
    #vanity URL
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-zA-Z0-9-_]*)$ /profile.php?username=$1 [L,QSA]
    RewriteCond %{THE_REQUEST} \ /profile\.php\?username=([^&\ ]+)&?([^\ ]*)
    RewriteRule ^ /%1?%2 [L,R=301]
    

    What I mean by the prefix for users is something like this:

    http://mysite.com/u/foobar
    

    Here, the /u/ indicates that "foobar" is a user, and not a php page. To do this, you'll need to add a few changes to the "# vanity URL" section:

    #vanity URL
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^u/([a-zA-Z0-9-_]*)$ /profile.php?username=$1 [L,QSA]
    RewriteCond %{THE_REQUEST} \ /profile\.php\?username=([^&\ ]+)&?([^\ ]*)
    RewriteRule ^ /u/%1?%2 [L,R=301]
    

    essentially including /u/ as part of the match and redirect.

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀