Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule URLs

         

nimonogi

5:33 pm on Apr 7, 2010 (gmt 0)

10+ Year Member



Hello,

I'm trying to create a rule that will rewrite URLs ending with "?currency=3" to a "/uk/" directory.

For Example:
1. domain.com/?currency=3 to domain.com/uk/
2. domain.com/computers.php?currency=3 to domain.com/uk/computers.php
3. domain.com/laptop/?currency=3 to domain.com/uk/laptop/
4. domain.com/laptop/hp.php?currency=3 to domain.com/uk/laptop/hp.php

I have created the following code but its not working as i want to:

RewriteRule ^uk/([^/]*)$ ./?currency=3 [L,NC]


Thanks for your help.

g1smd

6:42 pm on Apr 7, 2010 (gmt 0)

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



I'm trying to create a rule that will rewrite URLs ending with "?currency=3" to a "/uk/" directory.
The code you supplied actually accepts a URL request like
example.com/uk/<something>
and rewrites it to fetch content from a script at
/<indexfile>?currency=3
inside the server - and that difference is crucial.

Additionally, the rule allows infinite Duplicate Content because any URL request matching
www.example.com/uk/<anything>
will be accepted by the rule and content served. The NC flag further adds to the woe, by allowing aNyCase on those infinite URL variations.

I'd guess you would want a redirect such that if parameter-based URLs are requested the user is redirected to the 'friendly' URL - as well as a separate rewrite to accept the 'friendly' URL request and fetch content from the script.

However, I would guess that the
<something>
part of the
www.example.com/<something>
URL request varies, and you would want to serve different content for each variation?

nimonogi

7:48 pm on Apr 7, 2010 (gmt 0)

10+ Year Member




Additionally, the rule allows infinite Duplicate Content because any URL request matching www.example.com/uk/<anything> will be accepted by the rule and content served. The NC flag further adds to the woe, by allowing aNyCase on those infinite URL variations.


The problem with my RewriteRule is that every of the above examples shows the "domain.com/" content and not the content of the requested url.

All these will show the content of "domain.com/"
1. domain.com/uk/
2. domain.com/uk/computers.php
3. domain.com/uk/laptop/
4. domain.com/uk/laptop/hp.php

I want each rewriterule to show the content of the requested url.

g1smd

8:10 pm on Apr 7, 2010 (gmt 0)

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



You capture the path part of the URL request in the $1 backreference and then fail to use it when calling the script. That's likely the root cause of the problem.

Your rule fails to map everything in the URL request to the internal call to the script; that's what needs fixing. That's likely an extra parameter needed to be added on the end of the current script call.

jdMorgan

1:20 am on Apr 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Confusing...


1. domain.com/?currency=3 to domain.com/uk/
2. domain.com/computers.php?currency=3 to domain.com/uk/computers.php
3. domain.com/laptop/?currency=3 to domain.com/uk/laptop/
4. domain.com/laptop/hp.php?currency=3 to domain.com/uk/laptop/hp.php

Of the pairs on each line above, which is the URL that you want search engines to see, and which is the actual filepath inside the server?

Jim

nimonogi

6:27 am on Apr 8, 2010 (gmt 0)

10+ Year Member



The actual file path is handled by the script i'm using.

I'm dynamically displaying different content in each page based on the "?currency=3".

So i want to create a rewrite rule, that will display the content of, let's say, "domain.com/computers.php?currency=3" when the visitor/search engine
enters "domain.com/uk/computers.php"

g1smd

7:29 am on Apr 8, 2010 (gmt 0)

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



Now, this is the first time you have mentioned the word "computers" on both sides. Previously you didn't mention it in the script filepath. The devil in is every little detail.

One other thing. There is no need to have the ".php" part in the URL. This is the ideal time to go extensionless. The file on the server needs the ".php" part, but it doesn't need to appear in the URL.


Rewording...
I want to create a rewrite rule, that accepts a URL request for
example.com/uk/computers
and fetches content from the internal path
/computers.php?currency=3
inside the server.
Notice that one is a URL including domain name and the other is a server-internal filepath.

# Rewrite URL request for example.com/uk/<something> to script path /<something>.php?currency=3
# <something> can contain a-z 0-9 and - only, NOT a / or period
RewriteRule ^uk/([a-z0-9\-]+)$ /$1.php?currency=3 [L]


or

# Rewrite URL request for example.com/uk/<something> to script path /<something>.php?currency=3
# <something> cannot contain / or period
RewriteRule ^uk/([^/.]+)$ /$1.php?currency=3 [L]


Link to
href="/uk/<something>"
from the pages of your site.

Now this only handles URLs-with-pages directly in the UK folder. It will need modifying for use when the URL has a folder name as well as a page name, as well as catering for the folder-only URLs.

You'll likely need two rules, one for the
example.com/computer/hp
(contains slash, does not end with slash) style URLs - which will rewrite to the named file, and the other for the
example.com/computer/
(ends with slash) URLs - which will rewrite to the index.php file. The URL needs to contain "identifying features" so that the correct rule matches the incoming request. The rules must not match requests for robots.txt, image files, CSS or JS files, etc.

jdMorgan

12:01 pm on Apr 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> The actual file path is handled by the script I'm using.

Then in this case, the filepath *is* the script that you are using -- it is the 'target' filepath of the rewrites.

Jim

nimonogi

6:07 pm on Apr 8, 2010 (gmt 0)

10+ Year Member



Thanks for your help!

I have created the following rule and works as i want to, except when it is in directories...


RewriteRule ^uk/([^/]*)$ ./$1?currency=3 [L]


Working - domain.com/uk/
Working - domain.com/uk/computers.php
Not Working - domain.com/uk/laptop/netbooks/
Not Working - domain.com/uk/laptop/netbooks/hp.php

Any suggestion?

g1smd

12:42 am on Apr 9, 2010 (gmt 0)

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



Pattern something like:
uk((/[^/]+)+\.php)
though it is very likely you'll need two rules - each looking a different sets of requests.