Forum Moderators: phranque

Message Too Old, No Replies

rewrite rule for create dymanic url

error

         

xbl01234

10:32 am on Sep 16, 2007 (gmt 0)

10+ Year Member



Hi;
I want to write a rewrite rule for creating the dynamic url.
But it does not work.

it gave me the following error;
The requested URL /country/2/ was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an
ErrorDocument to handle the request.

And the requests is rewritten as follows:
domain.com/country/2/ -> domain.com/Category.php?page=2

public_html/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^/country/([0-9]+)/$ Category.php?page=$1[L]

and all the file i use here, they sit in the directory of the public_html.

when i click on the following link, it tell me the above error;
<a href="/country/2/"> click me </a>

Dilly

1:52 pm on Sep 16, 2007 (gmt 0)

10+ Year Member



.

[edited by: Dilly at 1:54 pm (utc) on Sep. 16, 2007]

g1smd

2:28 pm on Sep 16, 2007 (gmt 0)

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



As Dilly, said, you neeed to remove the leading "/" from the Rewriterule. Additionally, I would make sure that the rewrite did include the full path to the requested file beginning with a "/" there. Make sure there is a space before the [L] too!

RewriteRule ^country/([0-9]+)/$ /somepath/Category.php?page=$1 [L]

.

As for the "!-f" and "!-d" stuff, my guess is that it means that there is no rewrite called if there is a real file located at the "friendly URL" location.

So if /somepath/somefile.html exists, then that is what is directly served without any sort of rewrite.

.

Finally, do make sure that you have sorted out any non-www to www canonicalisation issues, earlier in the .htaccess file, BEFORE calling this rewrite.

jdMorgan

3:23 pm on Sep 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Once you have got the rule working, you also need to create the custom 404 error document that you (or your host) have defined using the "ErrorDocument 404" directive. This file is apparently missing, which is the cause of the "Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request" message.

Jim

g1smd

9:03 pm on Sep 16, 2007 (gmt 0)

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



Ooops. I forgot to mention that.

xbl01234

9:37 am on Sep 17, 2007 (gmt 0)

10+ Year Member



Hi G1smd;
Thanks it does works now.


Finally, do make sure that you have sorted out any non-www to www canonicalisation issues, earlier in the .htaccess file, BEFORE calling this rewrite.

For the above you mentioned, i have some information about the AuthName, AuthUserFile & AuthGroupFile in my .htaccess already, is that what you want me to do?

xbl01234

10:13 am on Sep 17, 2007 (gmt 0)

10+ Year Member



Hi Jim;


Once you have got the rule working, you also need to create the custom 404 error document that you (or your host) have defined using the "ErrorDocument 404" directive. This file is apparently missing, which is the cause of the "Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request" message.

For the above you mentioned, i have put the following line in my .htaccess file:
ErrorDocument 404 /404.html

and also the file of the 404.html is sitted on the /public_html/, i also wrote some thing into file. Is that you want me to do?

jdMorgan

1:05 pm on Sep 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, that's what you needed to do.

If you now request a non-existent page from your site, you should see your 404.html page displayed in your browser.

Jim

g1smd

8:09 pm on Sep 17, 2007 (gmt 0)

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



The non-www to www redirect is this one:

RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

and makes sure that only www URLs are indexed for the entire site.

xbl01234

9:07 am on Sep 18, 2007 (gmt 0)

10+ Year Member



Thanks Jim.

xbl01234

10:27 am on Sep 19, 2007 (gmt 0)

10+ Year Member




The non-www to www redirect is this one:

RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]

and makes sure that only www URLs are indexed for the entire site.

Thanks G1smd:

I 'd like to ask you some question:
1) without of your rewrite rule, the mod_rewrite create the dynamic url properly.
Do you just want to make 100% safty to creating dynamic url, so you advice me to add the
rewrite rule for the non-www to www redirect on?

2) what the "R=301" means?

3) If i want to create a rewrite rule which has two group parts from a rewrite rule,
for example, RewriteRule ^([^/]+)/([^/]+)/?$ foo.php?name1=$1&name2=$2.
Do i need to add another rewrite rule as the following:

RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^([^/]+)/([^/]+)/?$ [domain.com...] [R=301,L]

g1smd

10:32 am on Sep 19, 2007 (gmt 0)

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



The code I gave is a redirect, not a rewrite.

If a user requests a non-www URL from your site, the redirect forces the browser to make a new request for the www version of the page.

Once that has been done, your original rewrite can kick in and fetch the content from the dynamic filepath.

So, the code is an additional instruction that goes before your rewrite.

The [R=301] forces the server to issue an external 301 redirect, instead of performing an internal rewrite.

Your original rewrite will deliver content whether you request the www or the non-www URL. That is a Dupllicate Content problem. The preceding, new, redirect fixes the site so that non-www will redirect to www, and www will deliver the content via your rewrite.

xbl01234

9:35 am on Sep 20, 2007 (gmt 0)

10+ Year Member




The code I gave is a redirect, not a rewrite.

If a user requests a non-www URL from your site, the redirect forces the browser to make a new request for the www version of the page.

Once that has been done, your original rewrite can kick in and fetch the content from the dynamic filepath.

So, the code is an additional instruction that goes before your rewrite.

The [R=301] forces the server to issue an external 301 redirect, instead of performing an internal rewrite.

Your original rewrite will deliver content whether you request the www or the non-www URL. That is a Dupllicate Content problem. The preceding, new, redirect fixes the site so that non-www will redirect to www, and www will deliver the content via your rewrite.

Thanks for your helping.an i very appreciate about that