Forum Moderators: phranque

Message Too Old, No Replies

Replacing URL with mod_rewrite

mod_rewrite seems to be doing something...

         

chuender

12:51 am on Apr 23, 2004 (gmt 0)

10+ Year Member



Hi,

My .htaccess seems to be working as I can now access this page:

[localhost...]

by using this link:

[localhost...]

But when I go to click a link on the page, the URL now displays non-mod format again:

[localhost...]

Please help!

Thanks.

jdMorgan

3:06 am on Apr 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



chuender,

Welcome to WebmasterWorld [webmasterworld.com]!

The URL displayed when you hover over a link on a page cannot be changed by mod_rewrite, because mod_rewrite only changes URLs received in HTTP requests by the server before any content is serveb; Whatever URL is in the link code on the page will be displayed.

Or, if I misunderstand what you're saying, you may just have a bug in your RewriteRule. If so, post your code with the domain name changed to 'example.com' and let us have a look at it.

Jim

chuender

4:15 am on Apr 23, 2004 (gmt 0)

10+ Year Member



This is what's in my htacces:

RewriteEngine on
Options FollowSymlinks
RewriteBase /
RewriteRule ^\.htaccess$ - [F]
RewriteRule ^index/(.*)/(.*).php /index.php?fuseaction=$1.$2 [L]

It's ok that the hover links are not changed, but the links are not changed in the Address field on IE even when they are clicked on.

Please advise.

Thanks.

jdMorgan

4:44 am on Apr 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Speeding it up and cleaning it up just a little:

RewriteRule ^index/[b]([^/]*)/(.*)\.p[/b]hp /index.php?fuseaction=$1.$2 [L]

Will rewrite the "public" search-engine-friendly URIs and change them into query-string calls to your script. This happens entirely within your server, so the outside world is completely unaware of it. This is how most Webmasters want it to be - They do not want the 'index.php?fuseaction=abc.def' stuff showing in user's browser address bars or in the search engine listings.

If you really do want these query strings to show in browsers and search engine listings, then make it an external redirect:


RewriteRule ^index/([^/]*)/(.*)\.php http://www.example.com/index.php?fuseaction=$1.$2 [R=301,L]

but I *REALLY* don't suggest you do that! -- It defeats the whole purpose of making URIs user-friendyl and search-engine friendly.

Jim

chuender

5:08 am on Apr 23, 2004 (gmt 0)

10+ Year Member



Thanks Jim. I do not want the 'index.php?fuseaction=abc.def' stuff showing at the user address bar. I'll give this a try.

Thanks.

jdMorgan

5:26 am on Apr 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The trick is to change all the links on your pages to the friendly format, and then let mod_rewrite change those links, when requested, to the correct format to call your script.

Jim

chuender

2:58 pm on Apr 23, 2004 (gmt 0)

10+ Year Member



But does this mean I have to manually change my code to generate the friendly format?

isitreal

6:39 pm on Apr 23, 2004 (gmt 0)

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



Chuender, yes, I think it means you need to redo your internal links, that's what I wasn't getting either when I was trying to do this.

jdMorgan, thanks for explaining this again, I've been trying to get that working for a month, something in your explanation finally clicked. I have another question:

say you want to rewrite as above, but with the option of having either one or two items in the query string, like this:

rewriting to:
index.htm?section=something&page=4
or:
index.htm?section=something

I tried various ways to do that, and always ended up with a 500 error. Mod_rewrite is very hard to understand.

RewriteEngine On
RewriteRule ^(.*)/page(.*)\.htm /index.htm?section=$1&page=$2 [NC]

this works.

but this:
RewriteEngine On
RewriteRule ^(.*)/page(.*)\.htm /index.htm?section=$1&page=$2 [NC]
RewriteRule ^(.*)/ /index.htm?section=$1 [NC]

doesn't, I'm sure there's something basic wrong in how I'm doing it. In other words, I want an if type situation: if address is mysite.com/main/page0.htm then the first rewrite rule applies (this currently works), but if it's
mysite/main/ the second rewrite rule applies. Does this make sense?

jdMorgan

7:08 pm on Apr 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



isitreal,

Short answer: You may just need to use an [L] flag on your first rule, so that the rewrite engine quits if the first rule is applied. (Always use an [L] flag unless you have a known reason not to.)

Long answer: See this thread [webmasterworld.com] for one way to take apart a long friendly URL and build a complex query string, allowing for missing parameters and parameters in any order. This method depends on having "keys" in the friendly url, so that the parameter order can be discerned.

Jim

isitreal

1:52 pm on Apr 24, 2004 (gmt 0)

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



jdMorgan: thanks, I'll see if I can figure that out. This Mod_rewrite stuff is by far and away the hardest thing I've come across, the Apache documentation is incredibly dense and hard to understand, not nearly enough relevant explanations or examples like you give, thanks again for taking the time to help clarify. The other thread, as you noted, should help a lot of people, but it's still very difficult to actually understand what's happening, if it weren't for your postings I'd never have got it running at all.

chuender

2:38 am on Apr 25, 2004 (gmt 0)

10+ Year Member



jdMorgan,

I am tyring the example you linked 1373 to used keys for multiple parameters. This is what I'm trying to do:

Convert:
[localhost...]

To friendly:
[localhost...]

But this code is not working:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^index(.*)/parameter4_([^/]+)(.*)$ index$1$3?parameter4=$2 [QSA]
RewriteRule ^index(.*)/parameter3_([^/]+)(.*)$ index$1$3?parameter3=$2 [QSA]
RewriteRule ^index(.*)/parameter2_([^/]+)(.*)$ index$1$3?parameter2=$2 [QSA]
RewriteRule ^index(.*)/parameter1_([^/]+)(.*)$ index$1$3?parameter1=$2 [QSA]
RewriteRule ^index(.*)/([^/]*)/(.*)(.*) index$1$4?fuseaction=$2.$3 [QSA]
RewriteRule ^index /index.php [L]

Please advise.

Thanks.

Chuender

jdMorgan

3:16 am on Apr 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have an extra [QSA] on the first line, and the next-to-last rule looks too complicated.

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^index(.*)/parameter4_([^/]+)(.*)$ index$1$3?parameter4=$2
RewriteRule ^index(.*)/parameter3_([^/]+)(.*)$ index$1$3?parameter3=$2 [QSA]
RewriteRule ^index(.*)/parameter2_([^/]+)(.*)$ index$1$3?parameter2=$2 [QSA]
RewriteRule ^index(.*)/parameter1_([^/]+)(.*)$ index$1$3?parameter1=$2 [QSA]
RewriteRule ^index/([^/]+)/([^/]+) index?fuseaction=$1.$2 [QSA]
RewriteRule ^index /index.php [L]

If this does not work, I suggest you examine your error log to find out why. Just saying, "it doesn't work" really does not tell us very much, so it's hard to help.

Also, there are simpler ways to do this *if* your parameters are always presnet and always in the same order in the friendly URI. The above code is flexible, but slower.

Jim

chuender

5:56 am on Apr 25, 2004 (gmt 0)

10+ Year Member



Hi Jim,

Sorry about being vague - should have known to check the error log.

[Sat Apr 24 22:51:22 2004] [debug] mod_rewrite.c(1744): [client 127.0.0.1] mod_rewrite's internal redirect status: 0/10., referer: [localhost...]
[Sat Apr 24 22:51:22 2004] [debug] mod_rewrite.c(1744): [client 127.0.0.1] mod_rewrite's internal redirect status: 1/10., referer: [localhost...]
[Sat Apr 24 22:51:22 2004] [debug] mod_rewrite.c(1744): [client 127.0.0.1] mod_rewrite's internal redirect status: 2/10., referer: [localhost...]
[Sat Apr 24 22:51:22 2004] [debug] mod_rewrite.c(1744): [client 127.0.0.1] mod_rewrite's internal redirect status: 3/10., referer: [localhost...]
[Sat Apr 24 22:51:22 2004] [debug] mod_rewrite.c(1744): [client 127.0.0.1] mod_rewrite's internal redirect status: 4/10., referer: [localhost...]
[Sat Apr 24 22:51:22 2004] [debug] mod_rewrite.c(1744): [client 127.0.0.1] mod_rewrite's internal redirect status: 5/10., referer: [localhost...]
[Sat Apr 24 22:51:22 2004] [debug] mod_rewrite.c(1744): [client 127.0.0.1] mod_rewrite's internal redirect status: 6/10., referer: [localhost...]
[Sat Apr 24 22:51:23 2004] [debug] mod_rewrite.c(1744): [client 127.0.0.1] mod_rewrite's internal redirect status: 7/10., referer: [localhost...]
[Sat Apr 24 22:51:23 2004] [debug] mod_rewrite.c(1744): [client 127.0.0.1] mod_rewrite's internal redirect status: 8/10., referer: [localhost...]
[Sat Apr 24 22:51:23 2004] [debug] mod_rewrite.c(1744): [client 127.0.0.1] mod_rewrite's internal redirect status: 9/10., referer: [localhost...]
[Sat Apr 24 22:51:23 2004] [debug] mod_rewrite.c(1744): [client 127.0.0.1] mod_rewrite's internal redirect status: 10/10., referer: [localhost...]
[Sat Apr 24 22:51:23 2004] [error] [client 127.0.0.1] mod_rewrite: maximum number of internal redirects reached. Assuming configuration error. Use 'RewriteOptions MaxRedirects' to increase the limit if neccessary., referer: [localhost...]
[Sat Apr 24 22:51:23 2004] [error] [client 127.0.0.1] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: [localhost...]
[Sat Apr 24 22:51:23 2004] [debug] core.c(2694): [client 127.0.0.1] r->uri = /index.php, referer: [localhost...]
[Sat Apr 24 22:51:23 2004] [debug] core.c(2700): [client 127.0.0.1] redirected from r->uri = /index.php, referer: [localhost...]
[Sat Apr 24 22:51:23 2004] [debug] core.c(2700): [client 127.0.0.1] redirected from r->uri = /index.php, referer: [localhost...]
[Sat Apr 24 22:51:23 2004] [debug] core.c(2700): [client 127.0.0.1] redirected from r->uri = /index.php, referer: [localhost...]
[Sat Apr 24 22:51:23 2004] [debug] core.c(2700): [client 127.0.0.1] redirected from r->uri = /index.php, referer: [localhost...]
[Sat Apr 24 22:51:23 2004] [debug] core.c(2700): [client 127.0.0.1] redirected from r->uri = /index.php, referer: [localhost...]
[Sat Apr 24 22:51:23 2004] [debug] core.c(2700): [client 127.0.0.1] redirected from r->uri = /index.php, referer: [localhost...]
[Sat Apr 24 22:51:24 2004] [debug] core.c(2700): [client 127.0.0.1] redirected from r->uri = /index.php, referer: [localhost...]
[Sat Apr 24 22:51:24 2004] [debug] core.c(2700): [client 127.0.0.1] redirected from r->uri = /index.php, referer: [localhost...]
[Sat Apr 24 22:51:24 2004] [debug] core.c(2700): [client 127.0.0.1] redirected from r->uri = /index.php, referer: [localhost...]
[Sat Apr 24 22:51:24 2004] [debug] core.c(2700): [client 127.0.0.1] redirected from r->uri = /index/products/showMain.php, referer: [localhost...]

Why is it doing all these redirects?
These parameters are not always present, plus there may be more parameters in the future so I thought this is a good way to go.

Appreciate all your help.

Chuender.

chuender

2:30 am on Apr 26, 2004 (gmt 0)

10+ Year Member



I've simplified my code to have just 3 parameters:

[localhost...]

And this is what my mod_rewrite look like:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^index/([^/]+)/([^/]+)/([^/]+)$ index$1$3?by=$2
RewriteRule ^index/([^/]+)/([^/]+)/([^/]+)$ index$1$3?display=$2 [QSA]
RewriteRule ^index/([^/]+)/([^/]+)$ index?fuseaction=$1.$2 [QSA]
RewriteRule ^index index.php [L]

It works when I use:
[localhost...]

But it seems to loose the fuseaction when I use:
[localhost...]

Please advise.

Thanks.

jdMorgan

2:25 pm on Apr 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The most likely cause is the main difference in the regex patterns between what I posted and what your are trying to use. For example:

RewriteRule ^index(.*)/parameter4_([^/]+)[b](.*)[/b]$ index$1$3?parameter4=$2
RewriteRule ^index/([^/]+)/([^/]+)/[b]([^/]+)[/b]$ index$1$3?by=$2

Your pattern will correctly match only those URLs containing exactly three parameters separated by "/" -- any additional parameters will cause the match to fail.

Ref: [etext.lib.virginia.edu...]

Jim

chuender

8:45 pm on Apr 26, 2004 (gmt 0)

10+ Year Member



Hi Jim,

It FINALLY works, but not sure why...

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteBase /
RewriteRule ^index/(.*)/(.*)/(.*)/(.*)$?fuseaction=$1.$2&display=$3&by=$4
RewriteRule ^index/(.*)/(.*)/(.*)$?fuseaction=$1.$2&display=$3 [QSA]
RewriteRule ^index/([^/]+)/([^/]+)$ index?fuseaction=$1.$2 [QSA]
RewriteRule ^index index.php [L]

It was more like a game - I looked at $_SERVER['QUERY_STRING'] to figure out what was going on and changed my statements to get the output to match.

I this thing ugly or what?

Thanks for all your help, really appreciate it.

Chuender.