Forum Moderators: phranque

Message Too Old, No Replies

Need some help with URL rewriting

         

Nail_Yener

8:37 am on Oct 7, 2011 (gmt 0)

10+ Year Member



Hi,

I have been reading about URL rewriting and mod_rewrite for a while but it seems I cannot make it work as I want. Here is my website I am working on:

http://www.configureweb.com/


On this site I have different type of pages such as page.php, tutorial.php, post.php and category.php. Here is how the URLs are at the moment:

http://www.configureweb.com/page.php?page=about
http://www.configureweb.com/tutorial.php?tutorial=html-tutorial
http://www.configureweb.com/post.php?post=sample-post
http://www.configureweb.com/category.php?category=hosting-and-domains


and here is how I want them to be:

http://www.configureweb.com/page/about
http://www.configureweb.com/tutorial/html-tutorial
http://www.configureweb.com/post/sample-post
http://www.configureweb.com/category/hosting-and-domains


I tried something like the following code for pages:

RewriteRule ^page/([A-Za-z0-9-]+)/?$ page.php?page=$1 [L]


<a href="page/about">About</a>


It sends to the correct page but the stylesheet is not active and links on that page become like this:

www.configureweb.com/page/page/about/


What am I doing wrong?

[edited by: Nail_Yener at 8:53 am (utc) on Oct 7, 2011]

lucy24

8:43 am on Oct 7, 2011 (gmt 0)

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



It redirects to the correct page but the stylesheet is not active and links on that page become like this:

www.configureweb.com/page/page/about/

What am I doing wrong?

You are confusing Redirect and Rewrite. Further details will have to wait until it is not 1:43 AM ;-)

Nail_Yener

8:54 am on Oct 7, 2011 (gmt 0)

10+ Year Member



I knew someone would mention that word. I changed "redirect" to "send". I will be waiting further details :)

lucy24

6:11 pm on Oct 7, 2011 (gmt 0)

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



For starters, how come other people can edit their posts hours afterward while I get slapped with the "Time's Up!" message? :(

RewriteRule ^page/([A-Za-z0-9-]+)/?$ page.php?page=$1 [L]

<a href="page/about">About</a>

It sends to the correct page but the stylesheet is not active and links on that page become like this:

www.configureweb.com/page/page/about/

OK, all together now:

I really hate that damned machine
I wish that they would sell it
It never does just what I want
But only what I tell it


The missing stylesheet and the non-working relative anchors are classic symptoms of the Inadvertent Rewrite. But you cannot fault the RewriteRule, since it did exactly what you asked it to do.

RewriteRule ^page/([A-Za-z0-9-]+)/?$ page.php?page=$1 [L]

The absence of a [R] flag (in practice, almost invariably [R=301]) and/or full path both guarantee that the Rewrite will remain a Rewrite rather than changing into a Redirect. This means, briefly, that the server will offer up the text content of

www.example.com/page.php?page=blahblah

including any business performed by the php. But for all other purposes it "thinks" it is still at

www.example.com/page/blahblah

So any links in the form page.about sent it searching for

www.example.com/page/page.about

and so on.

It sounds as if you want the Redirect-plus-Rewrite two-step. The Redirect is to send the user to the place where all your content is really located. The Rewrite is to make the address look pretty. Fortunately this is the Hot Question of the Hour, so if you search this Forum for the package "rewrite redirect rewriterule" you will find many, may earlier threads. Or simply skim through the most recent page or so of posts.

g1smd

6:34 pm on Oct 7, 2011 (gmt 0)

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



The
RewriteRule
is almost correct.

The URL for a page should NOT end in a trailing slash. A URL with a trailing slash represents a folder or the index file within a folder.

Add a redirect before this rule to strip the slash if it is present in the request. This makes the URL "right". If you do not do this, you have Duplicate Content in your site: two different URLs that can directly serve the same content.

Delete the / and ? from your rule so it expects a request without a trailing slash. Now only one URL, one without a trailing slash, can directly serve the content.



When you link to
styles/stylesheet.css
from
www.example.com/page.php?page=about
the browser will ask for
www.example.com/styles/stylesheet.css


When you link to
styles/stylesheet.css
from
www.example.com/page/foo/bar/this
the browser will ask for
www.example.com/page/foo/bar/styles/stylesheet.css


This is caused by your relative link on your page. URLs in links are resolved by the BROWSER based on the current page URL. Use a root relative link on your page, one beginning with a leading slash, to fix this problem:
<a href="/styles/stylesheet.css">
.

Nail_Yener

7:42 am on Oct 8, 2011 (gmt 0)

10+ Year Member



lucy24: Thanks for the reply.

g1smd: URLs in links are resolved by the BROWSER based on the current page URL.


This answers many of my questions. Thank you very much! Now the links are just as I wanted. You can see this on the site.