简体   繁体   中英

Need PHP or Regex script for matching URL's

I'm changing all the URL's on my websites to lower case. I'm also replacing all the underscores with dashes. So the URL /World/New_York would become /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. 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).

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...

/world/new-york /World/New-York /WORLD/New York /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.

In my PHP code, the URL is represented by the value $MyURL. So if you visit MySite/world/new-york, $MyURL = new-york, where new-york is a MySQL database value.

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. 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?

$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...

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. When I change it to px/topics/Ethics or px/topics/ethics it works. 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. Rather, I'd like to see 32 hits for topics/ethics. Apache is way over my head, though. ;)

If you want to convert the URL's to lowercase automatically, use 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} .

Using PHP, you can just do strtolower() . Although, I don't quite understand why. I believe MySQL searches are case-insensative.

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 - .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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