Jump to content

URL Rewrite Not Working Properly


Go to solution Solved by Ch0cu3r,

Recommended Posts

I'm new to mod_rewrite. I'm trying to rewrite my URLs, but it's not working. I have changed the URLs on my website to look like this: 

<a href="/id_number/hyphenated_article_title/">Anchor_text</a>

E.g., 

<a href="/12/how-to-lose-weight-fast/">How to Lose Weight Fast</a>

My .htaccess file looks like this: 

RewriteEngine on
RewriteRule ^/([0-9]+)/[A-Za-z0-9-]+/?$ article.php?id=$1 [NC,L]

The rewrite is working, but article.php is loading slowly and without CSS styling.

Anyone know why?

Link to comment
https://forums.phpfreaks.com/topic/282412-url-rewrite-not-working-properly/
Share on other sites

I have to admit that I don't fully understand the flags that I have used.

 

"NC" means case-insensitive, which means that is doesn't matter if someone uses the following URL, right?

www.example.com/12/How-To-Lose-Weight-Fast/

They will still be served www.example.com/12/how-to-lose-weight-fast/. Is that correct?

 

But what exactly does "L" mean? I know it indicates that "the current rule should be applied immediately without considering further rules." My question is, what current rule?

All of the links on my website now begin like this:

www.example.com/12/how-to-lose-weight-fast/

So, for example, the URL for my contact page looks like this:

www.example.com/12/how-to-lose-weight-fast/contact-us.php

What is going on! How do I stop the rule from applying to every single URL on my website? Surely, it should only apply to URLs that begin with an ID number, right?

 

Any help will be much appreciated.

www.example.com/12/how-to-lose-weight-fast/contact-us.php

What is going on! How do I stop the rule from applying to every single URL on my website? Surely, it should only apply to URLs that begin with an ID number, right?

 

Any help will be much appreciated.

 

Thats not mod_rewrite doing it. Its the web browser. It thinks 12/how-to-lose-weight-fast/ is a directory, even though they dont exist. The web browser is appending contact-us.php to end of the current url. To prevent this start your urls with a / Example link.

<a href="/contact-us.php">Contact Us</a>

If a url starts with a / then the browser will load the link from the root of the site (http://site.com) regardless of what the current url is.

 

You'll want to have read about absolute and relative urls.

Edited by Ch0cu3r

What do you mean didn't work? Whats the code you're using for creating the contact-us.php link? What is the url that causes to not work correctly

 

I'm confused! Please let me explain my problem in more detail.

 

All of the links on my website are relative, like this:

<a href="contact-us.php">Contact Us</a>

All of the CSS paths on my website are relative, too, like this:

<link rel="stylesheet" type="text/css" href="style.css" />

I have links to videos on my website. They look like this:

<a href="watch-video.php?id=12">How to Lose Weight</a>

I want this link to look like this:

<a href="/12/how-to-lose-weight/"></a>

I have created a .htaccess file. It contains just these 2 lines:

RewriteEngine on
RewriteRule ^([0-9]+)/[A-Za-z0-9-]+/?$ watch-video.php?id=$1 [NC,L]

The rewrite is working. The page watch-video.php is loading with the right video, but the page has no styling. And when I hover over the URLs on watch-video.php, they all include /12/how-to-lose-weight/, like this:

<a href="www.mysite.com/12/how-to-lose-weight/contact-us.php">Contact Us</a>

Are you saying that my .htaccess file is fine and that this is all normal after a rewrite? Do I just have to change all paths to be absolute?

 

For example, do I have to do my video links like this:

<a href="http://www.mysite.com/12/how-to-lose-weight/">How to Lose Weight</a>
  • Solution

It is because your paths are relative. If you are on the page at http://site.com//12/how-to-lose-weight/ and you have link in your page like 

<a href="contact-us.php">Contact Us</a>

Then when you hover over the link you'll see the url becomes http://site.com//12/how-to-lose-weight/contact-us.php which is not what you want. If your links is

<a href="/contact-us.php">Contact Us</a>

And then hover over your link you'll see the link is point to the correct location http://site.com/contact-us.php. The / makes your urls absolute rather than relative.

 

relative urls get appended to what the current url (the page you're on for example http://site.com//12/how-to-lose-weight/).

 

You'll need to modify your existing urls (this includes links, stylesheets, images, javascripts etc) to work with your mod_rewrite urls. Otherwise you find issues like this. The simple work around is to start all urls with a / to make them absolute rather than relative.

It is because your paths are relative.

 

You'll need to modify your existing urls (this includes links, stylesheets, images, javascripts etc) to work with your mod_rewrite urls. Otherwise you find issues like this. The simple work around is to start all urls with a / to make them absolute rather than relative.

 

You were right, mate. ;D  I thank you for your help! Everything's fixed now.

 

I had to go through every page and change the URLs. :o

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.