简体   繁体   English

需要PHP或Regex脚本来匹配URL

[英]Need PHP or Regex script for matching URL's

I'm changing all the URL's on my websites to lower case. 我将网站上的所有URL更改为小写。 I'm also replacing all the underscores with dashes. 我还用破折号替换了所有下划线。 So the URL /World/New_York would become /world/new-york. 因此,URL / World / New_York将变为/ world / new-york。

However, I don't want to lose traffic from links that are still pointing to /World/New_York, so I'd like that link to work as well. 但是,我不想失去仍然指向/ World / New_York的链接的流量,因此我希望该链接也能正常工作。 As I understand it, I can use an Apache file to automatically change the URL's in the browser to the correct format (lower case). 据我了解,我可以使用Apache文件自动将浏览器中的URL更改为正确的格式(小写)。

So what I want to do right now is simply make sure that all relevant links are accepted, 1) regardless of case and 2) regardless of whether words are separated by _, - or a simple space... 所以我现在要做的就是确保所有相关链接都被接受,1)不管大小写,2)不管单词是用_,-还是简单的空格分隔...

/world/new-york /World/New-York /WORLD/New York /wORld/new_YORK / world /纽约/世界/纽约/ WORLD /纽约/ wORld / new_YORK

All of the above URL's should be accepted, because 1) they all have the same characters, and 2) the words are separated by a dash, underscore or space. 上面的所有URL均应接受,因为1)它们都具有相同的字符,并且2)单词之间用短划线,下划线或空格分隔。

In my PHP code, the URL is represented by the value $MyURL. 在我的PHP代码中,URL由值$ MyURL表示。 So if you visit MySite/world/new-york, $MyURL = new-york, where new-york is a MySQL database value. 因此,如果您访问MySite / world / new-york,则$ MyURL =纽约,其中纽约是MySQL数据库值。

So I think what I need to do is create a script that says $MyURL = 1) the database value, or 2) the database value with an allowance for alternate case values, and/or 3) the database value with the dash replaced by an underscore or space. 因此,我认为我需要做的是创建一个脚本,该脚本说$ MyURL = 1)数据库值,或2)数据库值,并允许备用大小写值,和/或3)数据库值,并用破折号代替下划线或空格。 I would then insert this script before my page display query (below). 然后,我将在页面显示查询(如下)之前插入此脚本。

Can anyone tell me how to write such a script using either PHP or regular expression? 谁能告诉我如何使用PHP或正则表达式编写此类脚本?

$sql= "SELECT COUNT(URL) AS num FROM orgs
WHERE URL = :MyURL";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':MyURL',$MyURL,PDO::PARAM_STR);
$stmt->execute();
$Total = $stmt->fetch();

@ Shahar... @沙哈尔...

Below is some code from one of my .htaccess files, where I inserted your code... 以下是我的.htaccess文件之一的一些代码,我在其中插入了您的代码...

RewriteEngine On
RewriteRule ^test\.htm$ test.php [L]
Options -MultiViews

#Your suggested rule...
RewriteRule ^/(.*)$ /${lower:$1}

php_flag magic_quotes_gpc Off

RewriteRule ^topics/([a-zA-Z0-9()_/-]+)/?$ topics/index.php?topic=$1 [L]
RewriteRule ^world/([a-zA-Z0-9()_/-]+)/?$ world/index.php?area=$1 [L]

But when I type in px/Topics/Ethics, I get a NOT FOUND error. 但是,当我输入px / Topics / Ethics时,出现了NOT FOUND错误。 When I change it to px/topics/Ethics or px/topics/ethics it works. 当我将其更改为px / topics / Ethics或px / topics / ethics时,它可以工作。 However, I'd like Ethics to change to ethics. 但是,我希望伦理学能够转变为伦理学。

One of my concerns is statistics. 我的关注点之一是统计。 I don't want to check my daily statistics and see 16 hits for Topics/Ethics, 13 for topics/Ethics and 3 for topics/ethics. 我不想查看我的每日统计数据,看到主题/道德的点击量为16,主题/道德的点击量为13,主题/道德的点击量为3。 Rather, I'd like to see 32 hits for topics/ethics. 相反,我希望看到32个关于主题/道德的命中。 Apache is way over my head, though. 但是,Apache使我头疼。 ;) ;)

If you want to convert the URL's to lowercase automatically, use mod_rewrite . 如果要自动将URL转换为小写,请使用mod_rewrite This is what you need to add: 这是您需要添加的内容:

RewriteMap lower int:tolower
RewriteRule  ^([^/]+)/?$  yourdir/${lower:$1}

You didn't specify what yourdir was... Or just do: RewriteRule ^/(.*)$ /${lower:$1} . 您没有指定yourdir是什么...或只是这样做: RewriteRule ^/(.*)$ /${lower:$1}

Using PHP, you can just do strtolower() . 使用PHP,您可以执行strtolower() Although, I don't quite understand why. 虽然,我不太清楚为什么。 I believe MySQL searches are case-insensative. 我相信MySQL搜索是不区分大小写的。

EDIT: Also, for the replacing part, I think this should work: 编辑:另外,对于替换部分,我认为这应该起作用:

RewriteRule ^(.*)\_(.*)$ $1\-$2
RewriteRule ^(.*)\%20(.*)$ $1\-$2

The first will replace _ with - , the second will replace space ( $20 ) with - . 第一个将取代_-第二个将替代空间( $20带) -

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM