Forum Moderators: phranque

Message Too Old, No Replies

Help with rewrite rule

         

sajison

9:01 pm on Jun 23, 2011 (gmt 0)

10+ Year Member



Hello, I am very new to this and am looking for some help. I am using interspire shopping cart that has a url that looks like:

http://www.example.com/products.php?product=item-name

I made this rewrite rule to make the url look like

http://www.example.com/item-name.html

RewriteRule ^/([^/]+)\.html products.php?product=$1

but it doesn't seem to work. Can someone point me in the right direction? would be very appreciative. thanks!


btw, we have not moved the nameservers yet so example.com is actually an IP address. I dont know if that makes any sort of difference.

g1smd

9:10 pm on Jun 23, 2011 (gmt 0)

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



A RewriteRule does not "make a URL like that". It can have no effect on the URLs shown in the links on your pages.

The first step is to link to the URL that you want users to see and use. Your RewriteRule comes into effect after the link is clicked. It accepts a URL request for a friendly URL and maps it to a non-default server internal filepath and file.

This is a regular question here, several times per week. As such there are several thousand previous detailed explanations with example code.

Add the [L] flag to the RewriteRule.

lucy24

9:14 pm on Jun 23, 2011 (gmt 0)

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



Rule #2 (Rule #1 is to use example.com, which you have done): Don't say "doesn't work". Spell out exactly what happens. If nothing seems to happen at all, say so.

Either I am reading your question upside-down or you have written it upside-down, because the RewriteRule would seem to do the exact opposite of what you want.

There are two different things:
the actual address of the page,
and what the user sees in their address bar.
A rewrite without redirect changes the first without changing the second. (A simple example is any 404 page, where your address bar gives the address of the nonexistent page you thought you were going to, while the physical page you're on is somewhere entirely different.)

g1smd

9:20 pm on Jun 23, 2011 (gmt 0)

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



There are two different things: URLs used out there on the web and filepaths and filenames here inside the server.

It is important to differentiate the two things. They are not at all the same thing, merely "associated" by the action of the server responding to requests.

sajison

4:10 pm on Jun 24, 2011 (gmt 0)

10+ Year Member



hi guys, thanks for the responses. let me try again being more clear hopefully ;)

so i have a site at

http://example.com/products.php?product=Organic-Apricot-Black-Tea

however i would like the user to see the following in the address bar.

http://example.com/Organic-Apricot-Black-Tea.html

i created this rewrite rule in the .htaccess file thinking that it would just change what the user sees in the address bar

RewriteRule ^/([^/]+)\.html products.php?product=$1

when uploaded, there was no affect in anyway that i noticed. the site works correctly just as if i had never created that rule.

so am i correct in understanding that the rewrite rule i created would change the actual address of the page and not what the user sees in the address bar if i dont create a redirect rule along with this?

@lucy24 the rewrite rule looks backwards? should i have written it like
RewriteRule products.php?product=$1 ^/([^/]+)\.html ?

i'll hunt around on these forums as well but if anyone could help me i would really appreciate it. maybe even a link to a good tutorial for a complete newb. thanks guys

lucy24

5:45 pm on Jun 24, 2011 (gmt 0)

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



There are two different things

OK, so there are 2x2 = four different things ;)

i created this rewrite rule in the .htaccess file thinking that it would just change what the user sees in the address bar

Thought so. You are doing it backward. Not structurally but conceptually. A rewrite_rule by itself doesn't change what the user sees; it changes where the user goes. So if you want the user to think they're in one place while they're really in another place, you need to:

#1 make all your links point to the apparent address
#2 use the rewrite rule to send people to the real address

and make sure it works both here-and-now (people clicking links on your site) and on the bigger scale (people bookmarking the page to come back later from somewhere outside your site).

sajison

6:35 pm on Jun 24, 2011 (gmt 0)

10+ Year Member



@lucy24

:) thanks! i'll try that out as soon as i get a moment. hehe u know how it is, million things to do, not enough time in the day...

g1smd

10:17 pm on Jun 24, 2011 (gmt 0)

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



To clarify the above.

Link to the URL you want people to see and use.

Set up a rewrite so that when they request that URL the request is rewritten to the correct intenal file.

Set up a redirect so that when they try to directly request a URL with parameters they are redirected to the new URL.

A rewrite is a URL to file translation (the target will be an internal file).

A redirect is a URL to URL translation (the target will be another URL).

URLs are used only out there on the web. Filepaths and files are used only here inside the server.

Both rules will use RewriteRule.

The redirect target will contain a domain name and will use the [R=301,L] flags.

The rewrite target will contain ONLY the server internal filepath and filename and use only the [L] flag.

sajison

3:15 pm on Jul 5, 2011 (gmt 0)

10+ Year Member



@g1smd

thanks for the clarification. so just to be clear, i need to create both rewrite and redirect rules so that the server as well as the outside users will be directed to the same place?

sajison

3:36 pm on Jul 5, 2011 (gmt 0)

10+ Year Member



edit above, by same place i mean that the server goes to the actual address (example.com?product=id) and the user goes to the address example.com/product

lucy24

7:48 pm on Jul 5, 2011 (gmt 0)

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



so just to be clear, i need to create both rewrite and redirect rules so that the server as well as the outside users will be directed to the same place?

You use a rewrite_rule to create a redirect. (But not vice versa.)

From the user's POV (stop foaming at the mouth, g1, your average user doesn't know from urls and pages, let alone servers and paths and absolutes):

RewriteRule user-request real-page [L]

>> user thinks they are at the place they asked for, but physically they are at place you sent them to. A simple example is an error page. The user's address bar will say {nonexistent-page} though physically they're on your 404 page. The only difference between this and other rewrites is that it's normally done automatically.

RewriteRule user-request real-page [R=301,L]

>> user gets magically sent to the right place and the browser's address bar changes.

sajison

2:41 pm on Jul 15, 2011 (gmt 0)

10+ Year Member



Hi guys, I managed to get this working. thanks for the help and patience with a newbie. ;)