php 请求url没有扩展名,apache – 将.php重定向到无扩展名,并在没有.php的情况下创建url...

这条规则:

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([a-zA-Z0-9-_.]*)$/profile.php?id=$1 [L]

需要在最后.

这条规则:

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}\.php -f

RewriteRule ^(.*)$$1.php

需要[L]标志并需要替换RewriteRule ^([^ /.])$$1.php [L].然后你可以复制这个并用.png和.txt以及你想要处理的每个其他扩展名替换.php,按优先顺序排列:

Options +FollowSymLinks -Multiviews

RewriteEngine on

# Redirect non-www to www:

RewriteCond %{HTTP_HOST} !^www\. [NC]

RewriteRule ^(.*)$http://www.%{HTTP_HOST}/$1 [R=301,L]

# Unless directory, remove trailing slash

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([^/]+)/$http://www.domain.com/$1 [R=301,L]

# Redirect external .php requests to extensionless url

RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/

RewriteRule ^(.+)\.php$http://www.domain.com/$1 [R=301,L]

# add extension if the php file exists:

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}\.php -f

RewriteRule ^(.*)$$1.php [L]

# add extension if the png file exists:

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}\.png -f

RewriteRule ^(.*)$$1.png [L]

# add extension if the txt file exists:

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}\.txt -f

RewriteRule ^(.*)$$1.txt [L]

# add extension if the html file exists:

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}\.html -f

RewriteRule ^(.*)$$1.html [L]

# etc.

# finally, route to profile.php if all else fails

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([a-zA-Z0-9-_.]*)$/profile.php?id=$1 [L]

ErrorDocument 404 /404.html

由于您将路由到profile.php,因此在profile.php脚本中,如果id参数不存在,则需要重定向到/404.html(意味着路由的URI必须是404).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值