Advanced Search

Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: .htaccess rewrite problem

  1. #1
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    243
    Thanks
    1
    Thanked 18 Times in 18 Posts

    Default .htaccess rewrite problem

    So I really don't know or understand much with htaccess. I am trying to do a simple url rewrite for a blog type thing. I want to take a url like amecms.com/view-article.php?article=whatever-whatever and make it show like amecms.com/article/whatever-whatever. I can get it to rewrite technically I guess but I think because of the added article/ in the url the page is not figuring out the directory correctly and isn't finding my css file and other files to include.

    You can check out what I mean amecms.com/articles.php and choose a article. Here is the rewrite rule I have so far.
    Code:
    RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteRule ^article/(.*)$ ./view-article.php?article=$1
    Any help is appreciated.

  2. #2
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    243
    Thanks
    1
    Thanked 18 Times in 18 Posts

    Default

    Oh how I love searching an trying for hours and finally posting for help and then 2 seconds later find a solution. I ended up using a <base> tag that tells files to use the path in the href of the tag for guidance. I set it to my main domain name and it all seems to work how I want.
    Code:
    <base href="http://www.example.com/" />
    All other pages seem to work fine too. I don't honestly know if this is the correct or best way to make it work, so I am very interested in what other methods to try.

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,624
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Using <base> can become problematic very easily. There's a good conversation here.

    I'm not saying you "shouldn't use it," or that it is a "bad thing" in general. Just make sure you know what it does, so you don't have any surprises.

    Here's what I would recommend:

    1. use RewriteBase in your htaccess. This does what you were probably trying to do, but only internally, when you're rewriting (no unexpected side effects).

    2. get rid of the leading ./ in your RewriteRule. It doesn't make sense there.

    3. test changes

    Code:
    RewriteEngine On
    
    RewriteBase /
    
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f
    
    RewriteRule ^article/(.*)$ view-article.php?article=$1
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  4. #4
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    243
    Thanks
    1
    Thanked 18 Times in 18 Posts

    Default

    Ok, tried that and it does the same thing as before I put in the <base>. I tried some variations of it with a url instead of the / but that didn't work either. Any other tricks?

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,624
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    alright; I see now what your issue is. You're using relative URLs to load your scripts.

    Yes, using <base> will fix that, though (as I mentioned) it may have other (unintended) side effects. But, if it works for your purpose, go ahead and use it.

    A better (IMHO) solution would be to use root-relative URLs where appropriate. For example, when you do <script src="scripts/general.js"></script>, you're always expecting to get the same script, right? The one in the scripts/ directory, off your document root, ...right?

    Problem is, that's not the URL you're asking for. You're asking for the scripts/ directory off of whatever directory you happen to be in at the moment. If your current URL is example.com/articles/some-article, you're asking for articles/scripts/general.js. If you're at example.com/how/did/i/get/here/im/so/confused/index.html, you're asking for /how/did/i/get/here/im/so/confused/scripts/general.js.

    If you want the scripts/ directory off your document root, ask for it specifically: <script src="/scripts/general.js"></script>.
    Last edited by traq; 06-30-2013 at 07:34 PM.
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  6. #6
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    243
    Thanks
    1
    Thanked 18 Times in 18 Posts

    Default

    Yeah after looking around I have found the / to be a better solution also. That too answers other questions I have had in my head. So I will have to take some time and change all the paths in the site. Thanks.

  7. #7
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    243
    Thanks
    1
    Thanked 18 Times in 18 Posts

    Default

    So after playing a little, the / method is great for live sites but makes it not work on local host. I have apache setup to read in my D: and then I have each site in a different folder. Browsing to the folder works just like browsing to a hosted site online. in the url bar it says localhost/whatever/file.php as an example. So now using the / the paths are looking at localhost as the root rather than the folder I have the site in.

  8. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,624
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    mm... different situations, different needs.

    ever thought about creating local domains on virtual hosts? There's a "learning curve," but it would allow each of your local sites to have its own document root. It's also very nice having a local environment that more closely resembles the "real" internet.
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  9. #9
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    243
    Thanks
    1
    Thanked 18 Times in 18 Posts

    Default

    I have been looking at that for the last few hours and have tried many times to make it work but with no luck.

  10. #10
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,624
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    What OS are you using?
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

Similar Threads

  1. rewrite single url via htaccess
    By chetanmadaan in forum Other
    Replies: 0
    Last Post: 10-03-2011, 06:56 PM
  2. .htaccess rewrite url problem
    By robert_gsfame in forum Other
    Replies: 3
    Last Post: 08-27-2010, 05:07 AM
  3. .htaccess help to rewrite URLs
    By Beverleyh in forum Looking for such a script or service
    Replies: 3
    Last Post: 06-11-2009, 12:18 PM
  4. htaccess mod rewrite
    By vineet in forum PHP
    Replies: 1
    Last Post: 12-15-2008, 11:06 AM
  5. .htaccess Rewrite URL
    By thanhnguyen in forum Other
    Replies: 2
    Last Post: 05-09-2007, 02:45 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •