Jump to content

Need some help with URL rewriting


ConfigureWEB

Recommended Posts

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 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?

Link to comment
Share on other sites

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

 

This is a relative path, assets requested by the browser will interpret this as relative to the current url. Since your rewrite makes it look like the file is one folder deep it's looking in the wrong place for the assets, you need to either use a full absolute URL or make them relative to root. In your case slapping a forward slash on the front of that should fix it.

Link to comment
Share on other sites

Also, there's one RewriteRule that'll cover all four examples, since they're all in the form /X/Y => X.php?X=Y:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([a-z]+)/(.*) $1.php?$1=$2 [L]

That'll cover any future cases that are also in the form above. But it can create false positives; if that's a problem then change the Rule to

RewriteRule ^(page|tutorial|post|category)/(.*) $1.php?$1=$2 [L]

(which means that the third Cond isn't needed anymore)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.