Forum Moderators: coopster

Message Too Old, No Replies

upper and lower case

         

Pico_Train

8:19 pm on Apr 25, 2008 (gmt 0)

10+ Year Member



Me thinks I have a bit of problem.

My pages are accessible from both

http://example.com/page

and

http://example.com/Page

How do I get around that little nugget?

[edited by: dreamcatcher at 6:22 am (utc) on April 26, 2008]
[edit reason] use example.com. Thanks. [/edit]

coopster

8:39 pm on Apr 25, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



What HTTP server are you running, IIS?

g1smd

7:31 pm on Apr 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It is unlikely to be an Apache problem, most likely IIS.

You "might" be able to fix it by adding URL detection and redirect code before your page content is processed within your scripts.

Pico_Train

4:02 pm on Apr 29, 2008 (gmt 0)

10+ Year Member



I am using Apache. Still haven't done anything on this as I had forgotten about the posting...was a little liquoured when I posted it.

Am also using .htaccess to rewrite the URLS.

The URLS will be dynamically generated so doing manually each time a new one gets added to the DB could become a bit tedious.

I am a bit stumped...

g1smd

6:47 pm on Apr 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You've set something up to be case-insensitive.

That could be in .htaccess or in your scripts.

PHP_Chimp

7:52 pm on Apr 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On a windows platform file names are case insensitive, on linux file names are case sensitive. So it may not be Apache, but the operating system that is running it.

So as page and Page are getting you to the same place I would guess that this is a windows OS system running Apache. As windows will treat page and Page as the same thing.
You could try Apache's mod_speling, as this should make URL's case insensitive. Although there must be a better method, so I would ask on the Apache forum.

As you are running through php could you not just use something like-


$page = strtolower($input);

As this would then stop your Page vs. page problem.

Pico_Train

7:58 pm on Apr 29, 2008 (gmt 0)

10+ Year Member



PHP_Chimp - I had just thought of this, which is actually quite obvious, and refreshed the page to see your reply! Just the kick up the back side I needed. I will give it a go tomorrow as it's getting a bit late now...

Pico_Train

8:08 pm on Apr 29, 2008 (gmt 0)

10+ Year Member



Nope had a quick try, that doesn't work...

Could I do a 301 redirect with .htaccess to redirect all uppercases to lower case urls - using a wildcard or what not? I'm very good with .htaccess...

PHP_Chimp

9:53 pm on Apr 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What rewrite rule/s are you using at the moment?

Pico_Train

7:50 am on Apr 30, 2008 (gmt 0)

10+ Year Member



I got this from webmasterworld too ad tweaked it for my own needs...

# Internally rewrite search engine friendly static URL to dynamic filepath
#and query
RewriteRule ^town/([^/]+)/?$ accommodation.php?town=$1 [L]
RewriteRule ^town/([^/]+)/([^/]+)/?$ accommodation.php?town=$1&company=$2 [L]

# Externally redirect client requests for old dynamic URLs to equivalent new static URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /accommodation\.php\?town=([^\ ]+)\ HTTP/
RewriteRule ^accommodation\.php$ http://www.example.com/accommodation/town/%1? [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /accommodation\.php\?town=([^&]+)&company=([^\ ]+)\ HTTP/
RewriteRule ^accommodation\.php$ http://www.example.com/accommodation/town/%1/%2? [R=301,L]

I am also querying MySql to find a result set WHERE town like "%$_GET['town']%" - wonder if that has anything to do with it too.

[edited by: dreamcatcher at 8:59 am (utc) on April 30, 2008]
[edit reason] use example.com. Thanks. [/edit]

Pico_Train

7:56 am on Apr 30, 2008 (gmt 0)

10+ Year Member



In addition to all of the above - my DB table for the company has two fields for the company name -

company name - normal stored as - John's Widget Company

and

company_name_fix - a "fix" stored as - johns-widget-company

I use the fix in the url anchor tag but if I change all the first characters, and I suspect the entire case of the word to Johns-Widget-Company or JOHNS-WIDGET-COMPANY - it works fine.

I was hoping an .htaccess fix could switch case of url and 301 any caps urls to the lower case version of the company name that could be 1, 2,3,4,5,6 or more words.

Am I clear or just confusing the issue?

Pico_Train

8:02 am on Apr 30, 2008 (gmt 0)

10+ Year Member



Could I do a 301 redirect with .htaccess to redirect all uppercases to lower case urls - using a wildcard or what not? I'm very good with .htaccess...

Um, major typo there! I am NOT very good with .htaccess.

g1smd

8:18 pm on Apr 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'd say that you would be better to have these fixes in your scripts, with the redirects sent within the headers.

I wouldn't use .htaccess for this.

PHP_Chimp

11:05 am on May 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I also think that you would be better fixing this at the script level; if for no other reason than it allows customers to enter Place or place into there search (as I assume that is what you are using this for).

As you are searching for "%$_GET['town']%" that will give you-any number of characters, followed by the GET string, followed by any number of characters.
So if you used strtolower($_GET['town']); then you could guarantee that both Place and place will result in the same query.

If you want all of the GET array in lowercase then you can use array_walk [uk3.php.net] to speed up the process.

Pico_Train

12:19 pm on May 1, 2008 (gmt 0)

10+ Year Member



It's not actually for the search I would like to do this, I am already doing strtolower for searches I use. I also use the town names in lower case in hyperlinks I echo out, but that's a not problem, because that's in my control.

I am concerned about when a user might enter the url in the address bar as [site.com...]

The url stays as that - [site.com...]

I'd like the url to change to

[site.com...]

That way I can avoid uppercase manual input and uppercase links people might create unknowingly or maliciously. To avoid duplicate content issues.

jdMorgan

1:11 pm on May 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I also think that you would be better fixing this at the script level.

I agree. The only solution available in .htaccess is a very slow and inefficient character-by-character rewrite. In addition, multiple case-conversion rewrites trigger a bug in Apache mod_rewrite, which must be worked-around, further reducing efficiency.

I'd recommend that you detect incorrect case in your script, and generate and send a 301-Moved Permanently header with the correct URL.

Jim

PHP_Chimp

1:16 pm on May 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are already forcing lower case strings in your search then is this a problem?

As allowing a person to type London or london will make no difference to the end result of your search.
Bots will only index pages that you are linking to, so you will be internally linking to pages with lower case names. So there will be no issue with duplicate content, unless other people link to your site with upper case names.

If you want to 'force' lower case names then you could run the GET string through a filter and


if ($_GET['town'] !== strtolower($_GET['town'])) {
header(Location: strtolower($_GET[town']), true, 301);
}

As this will redirect a person who types in London to the london page. The 301 redirect is to catch bots coming from other peoples links.

Is that a bit more what you wanted?

<edit>
Well I was going to suggest asking on the apache forum...but I think the above says that it isnt worth it.

[edited by: PHP_Chimp at 1:17 pm (utc) on May 1, 2008]

Pico_Train

2:55 pm on May 1, 2008 (gmt 0)

10+ Year Member



yes, I see, that looks rather good! Will give it a try, thank you everyone for every bit of help you have given me, hopefully, one day I can return the favour but I think you are a few steps ahead of me!

Pico_Train

3:03 pm on May 1, 2008 (gmt 0)

10+ Year Member



Simply the best! Perfect!

Pico_Train

3:52 pm on May 1, 2008 (gmt 0)

10+ Year Member



Just one more thing...

Why are we doing double == signs like this

!==

and not

!=

What is the difference there?

jdMorgan

4:37 pm on May 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> If you are already forcing lower case strings in your search then is this a problem?

Two words: "Duplicate content"

Each page on your site should be directly-accessible via one and only one URL. Any and all variations of that canonical URL should result in a 301-Moved Permanently redirect to the canonical URL.

Jim

g1smd

6:18 pm on May 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Use = when setting a variable to some value.

Use == when comparing two variables/values.

Pico_Train

8:36 pm on May 1, 2008 (gmt 0)

10+ Year Member



g1smd - ok cool, gotcha.

jdMorgan - exactly, that's why I was so intent on rewriting those urls, which I have now done with a couple extra if statements to check he urls.

Thanks for your help!

PHP_Chimp

9:29 am on May 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you compare [uk.php.net] variables you can either use == or === (or != or !==, for the negative version).

== means same as, so 2 == '2' (the integer 2 and the string 2) are the same.
=== means identical, so 2 === '2' is wrong. As although they are the same value they are not of the same type (1 is an integer the other is a string).

I generally try to use identical when testing variables. Although not really necessary, it is just something that I do.

sc0ttkclark

4:40 pm on May 16, 2008 (gmt 0)

10+ Year Member



There must be an htaccess rewrite rule that would accomplish this for a WordPress installation:

Theoretic code but not functional of course:

RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^index.php/(.*)$ {lower:$1} [R=301,L]

PHP_Chimp

11:43 am on May 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From jdMorgan in the comments above.
The only solution available in .htaccess is a very slow and inefficient character-by-character rewrite. In addition, multiple case-conversion rewrites trigger a bug in Apache mod_rewrite, which must be worked-around, further reducing efficiency.

So you could do it with htaccess...but it is slow and triggers a bug. Or you could just stick a couple of lines of additional php into the code to solve the problem.

Of course you can choose either method, although I know which one I would choose ;)

Pico_Train

8:15 pm on May 17, 2008 (gmt 0)

10+ Year Member



I've done it with php if statements checking the case and redirecting with strtolower if necessary and it works like a charm. 301 redirect of course :)

What I have also added is to check that the $_GET also returns a result set or else send a 404 and then I also check in another case that the $_GET is numeric and if not, return a 404. If it is numeric, get the result array, if result array is empty then throw a 404, if not display, so I have a whole battery of checks!

The only thing I am struggling with now is, my page only works if example.php?id=4 where 4 is a valid place. If you go to example.php on it's own it just displays a blank page which I would like to avoid. Working on that but I am also working on this beer so the beer takes precedent for now...

[edited by: Pico_Train at 8:16 pm (utc) on May 17, 2008]

g1smd

7:39 pm on May 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That's easy. Test for a blank query string.

Actually, test for "nothing usable" in the query string, so that you can bounce requests with random junk presented in the query string.