doucha7329 2017-10-30 09:30
浏览 51
已采纳

如何在使用htaccess和php重写用户配置文件页面时允许使用dot

I have encountered a problem in trying to rewrite users profile page with usernames containing a full stop.

First of all, i applied this rewrite rule to my htaccess file;

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_]+)$ profile.php?tg_u=$1 [NC,L]
RewriteRule ^([^.]+)$ $1.php [NC,L]

actually, the rewrite rule worked when accessing users with username containing only letters and username containing a number and underscore(_) but when trying to access users with username such as user.name i do get a 404 page error, i tried changing previous preg_match to this preg_match(a-zA-Z0-9_.) and later to this preg_match(a-zA-Z0-9_.) on my htaccess (rewrite rule) but i noticed that when i want to access my site in localhost, the profile.php page is been shown as index page, i later then use this preg_match(a-zA-Z0-9.) on my htaccess file but still encounter the same problem.
I will be very grateful to know why i encounter this problem and also how to fix it.

  • 写回答

1条回答 默认 最新

  • dsgm5631 2017-10-30 12:22
    关注

    You can use:

    RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteRule ^([a-zA-Z0-9_.]+)$ profile.php?tg_u=$1 [NC,L]
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^([^.]+)$ $1.php [NC,L]
    

    -d If the request is not a directory, and -f not a valid file name.
    And I added a test not to rewrite to php a page that does not exist.

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

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动