逃避mod_rewrite的正则表达式句点(。)字符 [英] escape period (.) character in mod_rewrite regex

查看:216
本文介绍了逃避mod_rewrite的正则表达式句点(。)字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个设置了这个规则.htaccess文件:

I currently have a .htaccess file set up with this rule:

重写规则^([A-ZA-Z0-9 _-] +)$ USER \的.php?用户= $ 1 [L]

而按预期工作: mysite.com/joe 变成 mysite.com/user.php?user=joe

which works as expected: mysite.com/joe becomes mysite.com/user.php?user=joe

我如何可以改变正则表达式包括在可能的用户名段?

How could I change the regex to include periods in a possible username?

例如,我想 mysite.com/joe.bloggs 即可转到 mysite.com/user=joe.bloggs

For example I would like mysite.com/joe.bloggs to go to mysite.com/user=joe.bloggs

目前这带来了一个404缺页错误。

Currently this brings up a 404 missing page error.

我已经试过 ^([A-ZA-Z0-9 \ ._-] +)$ USER \的.php?用户= $ 1 [L] 但是,这会产生一个内部错误500(我认为这是由于一个无限循环:user.php的目的是重定向到网页,如果没有指定用户)

I have tried ^([a-zA-Z0-9\._-]+)$ user\.php?user=$1 [L] But this produces an internal error 500 (I think this is due to an infinite loop: user.php is designed to redirect to the homepage if no user is specified.)

我是pretty的新的对所有这一切的(特别是正则表达式)。感谢您的帮助!

I'm pretty new to all of this (regex especially). Thanks for any help!

推荐答案

首先检查请求的URL不是一个文件或文件夹。这将prevent的重定向循环的ULS像 /user.php

First check if the requested urls is not a file or folder. This will prevent the redirect loop for uls like /user.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-.]+)$ user.php?user=$1 [L,QSA]

这篇关于逃避mod_rewrite的正则表达式句点(。)字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆