Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite help (noob)

         

mandark

12:02 pm on Mar 4, 2006 (gmt 0)

10+ Year Member




I would like the following

[someserver.com...]

to be converted to

[someserver.com...]

I am writing the following:

RewriteRule ^game/(.*)/(.*)$ showinfo.php?id=$2

This works. However the images on the showinfo.php page would not show up!

Following the complete ".htaccess" file:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

# [someserver.com...]

RewriteRule!^showinfo\.php - [C]
RewriteCond %{QUERY_STRING} ^.+$
RewriteRule .* showinfo.php [QSA,L]

# [someserver.com...]

RewriteRule ^game/(.*)/(.*)$ showinfo.php?id=$2

Please help!

jdMorgan

4:16 pm on Mar 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mandark,

Welcome to WebmasterWorld!

You'll need to make sure that all of your images, stylesheets, and external JavaScripts are referenced by either a server-relative URL or a canonical URL, and not by a page-realtive URL.

That is, use <img src="/images/logo.gif"> or <img src="http://www.example.com/images/logo.gif">, and not <img src="images/logo.gif> or <img src="../images/logo.gif">.

An alternative is to add code to also rewrite the images along with the "pages" -- This would allow the page-relative links to work.

In order to make sense of this, remember that it is the client --the browser-- that resolves relative links.

Here is a clean-up of your rules to speed things up a bit:


RewriteCond %{QUERY_STRING} .
RewriteRule !^showinfo\.php$ showinfo.php [L]
#
RewriteRule ^game/([^/]+/(.*)$ showinfo.php?id=$2

Jim

[edited by: jdMorgan at 4:17 pm (utc) on Mar. 6, 2006]

mandark

9:33 am on Mar 5, 2006 (gmt 0)

10+ Year Member



Thanks Jim,

Your tip on the image and css worked :)

However, the modified code that you gave for .htaccess didn't.

I undertand my earlier code needs fine tuning, can you please explain me what's the difference in the two codes?

Thanks.

jdMorgan

4:30 pm on Mar 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




# OLD code
# If the requested URL is NOT showinfo.php, chain to next rule
RewriteRule !^showinfo\.php - [C]
# If query string is non--blank
RewriteCond %{QUERY_STRING} ^.+$
# Rewrite all requests (except for showinfo.php) to showinfo.php
RewriteRule .* showinfo.php [QSA,L]
#
# Rewrite requests for /game/<anything_1>/<anything_2> to showinfo.php?id=<anything_2>
RewriteRule ^game/(.*)/(.*)$ showinfo.php?id=$2


# NEW code
# If query string is non-blank
RewriteCond %{QUERY_STRING} .
# Rewrite all requests (except for showinfo.php) to showinfo.php
RewriteRule !^showinfo\.php$ showinfo.php [L]
#
# Rewrite requests for /game/<anything_1>/<anything_2> to showinfo.php?id=<anything_2>
RewriteRule ^game/([^/]+/(.*)$ showinfo.php?id=$2 [L]

The only difference is that the edited code is more efficient. Well, that *should be* the only difference. However, since I'm not familiar with your URL structure, it could be that the second rule needs to be changed if there can be more than two slashes after 'game'. The change would be:

RewriteRule ^game/(([^/]+/)+(.*)$ showinfo.php?id=$3 [L]

The goal with this modified rule is to avoid using a pattern with more than one ".*" subpattern in it. Using more than one of these patterns is highly inefficient, and a forward-looking negative match pattern as shown will run much faster.

Jim

mandark

11:15 am on Mar 7, 2006 (gmt 0)

10+ Year Member



Hello Jim,

Thanks for your explanation. Some of it is still greek to me :)

Let me explain my requirement:

I have a web page which takes in one parameter. Which looks as follows

[someserver.com...]

Now, I would like this page to be accessible using the following two URLs.

[someserver.com...]
[someserver.com...]

In the second one "Be-Bice-American" is based on the data value of a table, so it can change.

What would be the most efficent approach to this? Can you please help me?

If you want I can PM you the site info.

Thanks.

jdMorgan

3:19 pm on Mar 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Without knowing all of the details of how far and wide your URL field values vary, all I can suggest is a point solution for rewriting URLs of the form

/<letters or hyphens>/<numbers> followed by single optional slash

Which would be:


RewriteRule ^[a-z\-]+/([0-9]+)/?$ /showinfo.php?id=$1 [NC,L]

However, it's up to you to understand this and adapt it to your needs, as we are primarily a discussion forum here, and try to avoid being a "help desk."

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Also, unless your site is *very* different from most, you *do not* want the same content accessible at two URLs, static and dynamic. Rather, you want to externally redirect client requests for the dynamic URL to the static URL, in order to consolidate your search engine listings and avoid so-called "duplicate-content penalties." This is an advanced subject compared to the above, so get the static-to-dynamic rewrite(s) working first, and then we can address the dynamic-to-static redirect problem more efficiently.

Jim

mandark

7:05 am on Mar 8, 2006 (gmt 0)

10+ Year Member



Hello Jim,

Thanks for the links to the Apache forum section.

The solution that you gave worked great. I made some changes to it. Instead of numeric paramater, I wanted to send alphanumeric value.

So I changed

RewriteRule ^[a-z\-]+/([0-9]+)/?$ /showinfo.php?id=$1 [NC,L]

to

RewriteRule ^[a-z\-]+/([a-z0-9]+)/?$ /showinfo.php?id=$1 [NC,L]

It works. But, I hope I used this correctly?

Now, I would like this URL also to be accessible via a smaller link, like [mysite.com...]

The idea is, the middle part of my URL is like

[mysite.com...]

where, the "this-can-be-a-long-string" can be of any length, so while mailing or other sites refering to this URL, I want to have a shortened version.

I am writing this as:

RewriteRule ^/?=([a-z0-9]+)$ /showinfo.php?id=$1 [NC,L]

But it is not working.

Thanks.

jdMorgan

4:22 pm on Mar 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Query strings are not part of the URL; They are data attached to a URL. Therefore, you cannot test a query string in a RewriteRule.

You must use something like


RewriteCond %{QUERY_STRING} ^some_query_string_value$
RewriteRule ^some_URL$ /new_URL [L]

instead.

Jim

[edit] speling [/edit]

[edited by: jdMorgan at 11:57 pm (utc) on Mar. 9, 2006]

mandark

4:47 am on Mar 9, 2006 (gmt 0)

10+ Year Member



Hi Jim,

Thanks again for your help!

This one is working for me.

RewriteCond %{QUERY_STRING} ^id=(.*)
RewriteRule ^$ /showgame.php?id=%1 [L]

Can you tell if this is optimized?

Cheers.

jdMorgan

11:59 pm on Mar 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That looks OK, as long as you're aware that it will accept a blank value for "id=". To avoid that, use (.+) instead of (.*), or make it even more specific, like ([a-z0-9]+) or something.

Jim

Pop_Black

1:56 pm on Apr 10, 2006 (gmt 0)

10+ Year Member



Hi jdMorgan & other members

I have this problem too,

[localhost...] script/?a=details&lid=1234
to
[localhost...] script/detail/1234

RewriteRule ^detail/([0-9]+)/?$?a=details&lid=$1
However the images & css not shown in [localhost...] script/detail/1234
& links shown in /detail/ for example
/my script/home.html shown in /my script/detail/home.html
I can,t solve it whith mod_rewrite,
Please Guide Me

jdMorgan

12:23 am on Apr 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you expecting mod_rewrite to change your links? It won't do that. It will only change the server filepath assoiciated with URLs requested from your server.

In other words your code does not rewrite
http://localhost/my script/?a=details&lid=1234
to
http://localhost/my script/detail/1234

In fact, it rewrites the requested URL
http://localhost/my script/detail/1234
to the file
http://localhost/my script/?a=details&lid=1234

If you want to change the links on your pages, you have to edit those pages -- or edit the script that generates those pages.

As for the broken images and CSS, the reason is that you are most likely using relative links, and it is the browser that resolves relative links. The browser still thinks the page is in the subdirectory /detail, so it will resolve (for example) the relative URL in the image tag <img src="logo.gif"> to the canonical URL http://example.com/detail/logo.gif.

To fix this, you can either move those images and CSS files into a real subdirectory called /detail, or you can use server-relative or canonical URLs in links to those files. Examples: <img src="/logo.gif"> or <img src="http://www.example.com/logo.gif">.

For more information, see the mod_rewrite tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

Pop_Black

10:30 am on Apr 11, 2006 (gmt 0)

10+ Year Member



Special Thanks Jim,

I Thought we can change image folder path for detail with mod_rewrite rules ...

I Have another problem,

I want change
[localhost...] script/?a=go&lid=1234
to
[localhost...] script/go.html?id=1234
I write this rule
RewriteRule ^go.html?id=([0-9]+)$?a=go&lid=$1
But it don,t work

I think "?" have a problem because below rule with "/" worked correctly

[localhost...] script/?a=go&lid=1234
to
[localhost...] script/go.html/id=1234
RewriteRule ^go.html/id=([0-9]+)$?a=go&lid=$1

jdMorgan

3:25 pm on Apr 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Query strings attached to a URL are not seen by RewriteRule.

You must use RewriteCond and examine the {QUERY_STRING} variable:


RewriteCond %{QUERY_STRING} ^a=go&(lid=[0-9]+)$
RewriteRule ^my_script/go\.html$ http://example.com/my_script/go.html?%1 [R=301,L]

Jim