Fluoresce Posted September 24, 2013 Share Posted September 24, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/282412-url-rewrite-not-working-properly/ Share on other sites More sharing options...
Fluoresce Posted September 24, 2013 Author Share Posted September 24, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/282412-url-rewrite-not-working-properly/#findComment-1451044 Share on other sites More sharing options...
Fluoresce Posted September 24, 2013 Author Share Posted September 24, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282412-url-rewrite-not-working-properly/#findComment-1451051 Share on other sites More sharing options...
Ch0cu3r Posted September 24, 2013 Share Posted September 24, 2013 (edited) 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 September 24, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282412-url-rewrite-not-working-properly/#findComment-1451070 Share on other sites More sharing options...
Fluoresce Posted September 24, 2013 Author Share Posted September 24, 2013 To prevent this start your urls with a / Example link. <a href="/contact-us.php">Contact Us</a> Thanks but didn't work. Anyone else? Quote Link to comment https://forums.phpfreaks.com/topic/282412-url-rewrite-not-working-properly/#findComment-1451073 Share on other sites More sharing options...
Ch0cu3r Posted September 24, 2013 Share Posted September 24, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/282412-url-rewrite-not-working-properly/#findComment-1451074 Share on other sites More sharing options...
Fluoresce Posted September 24, 2013 Author Share Posted September 24, 2013 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> Quote Link to comment https://forums.phpfreaks.com/topic/282412-url-rewrite-not-working-properly/#findComment-1451079 Share on other sites More sharing options...
Fluoresce Posted September 24, 2013 Author Share Posted September 24, 2013 Instead of going through my entire website and changing every path to absolute, isn't there an edit I can make to my .htaccess file? I thank you for your attention. Quote Link to comment https://forums.phpfreaks.com/topic/282412-url-rewrite-not-working-properly/#findComment-1451081 Share on other sites More sharing options...
Solution Ch0cu3r Posted September 24, 2013 Solution Share Posted September 24, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282412-url-rewrite-not-working-properly/#findComment-1451086 Share on other sites More sharing options...
Fluoresce Posted September 25, 2013 Author Share Posted September 25, 2013 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. I thank you for your help! Everything's fixed now. I had to go through every page and change the URLs. Quote Link to comment https://forums.phpfreaks.com/topic/282412-url-rewrite-not-working-properly/#findComment-1451130 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.