Forum Moderators: phranque
[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!
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
[edited by: jdMorgan at 4:17 pm (utc) on Mar. 6, 2006]
# 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]
RewriteRule ^game/(([^/]+/)+(.*)$ showinfo.php?id=$3 [L]
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.
/<letters or hyphens>/<numbers> followed by single optional slash
Which would be:
RewriteRule ^[a-z\-]+/([0-9]+)/?$ /showinfo.php?id=$1 [NC,L]
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
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.
You must use something like
RewriteCond %{QUERY_STRING} ^some_query_string_value$
RewriteRule ^some_URL$ /new_URL [L]
Jim
[edit] speling [/edit]
[edited by: jdMorgan at 11:57 pm (utc) on Mar. 9, 2006]
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
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
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