SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    .htacccess for a specific url

    Hi There,

    I'm building a site using opencart and every page on the site has an seo-friendly url except for one, which is the main page of the blog. The url for this is: /index.php?route=blog/category/home/index.php

    I'd like this to be just plain old /blog but I can't seem to get it to do this. The opencart forums are about as useful as a wet flannel, so I thought I'd come here to seek the answer to this query.

    Here's what's currently in the .htaccess file:

    Code:
    # SEO URL Settings
    RewriteEngine On
    # If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/ 
    
    RewriteBase /1521/
    RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
    RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
    RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
    So, any ideas how I can just make a specific request in the .htaccess to change the aforementioned url to /blog?

    Thank you,
    Dan

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Dan,

    What have you tried? Have you read the (lengthy) mod_rewrite tutorial linked in my signature or the sticky posts which were derived from the tutorial? The answer to your question is trivial but I have a real reluctance to code for "script kiddies" (no offense intended, I'm just trying to verify for myself that you're not one of them).

    As for the code you do have, WHY the RewriteBase directive and do you know what the last RewriteRule's regex is asking (it looks stoo-pid)?

    I'll be happy to help but please address the above.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi DK,

    I've been a freelance web designer for about five years, but this has mostly involved plain old html and css. I have delved into the .htaccess file many times, but this has been largely just to set up redirects - I can't deny that the mod_rewrite is something I know little about. I've tried cutting and pasting from other forum posts and tutorials and I understand that many of the rules are related to page ids and directory structure (correct me if I've misunderstood this ) but this one url doesn't contain a page id for me to target. I'd like to read your tutorial, but when I try to visit that page it says "Reserved ...".

    The code already in place is what comes with opencart by default, and I'm fully aware that they aren't perfect coders, and I had to completely rebuild their jquery tabs system due to the aweful way it was structured.

    To answer your last two questions: I'm using this because I thought it makes sense to use what is already built in to opencart, and I don't know what the last RewriteRule's regex is asking.

    Thanks,
    Dan

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Dan,

    Thanks for the info about the failure of my mod_rewrite code on my own website. Actually, there must be something in the script itself because all the other links work fine as does the http://datakoncepts.com/SEO link (seo is not capitalized) so I've updated my SitePoint signature and the mod_rewrite to get you to the script. Should that fail for you, link directly to the http://datakoncepts.com/seo.php script.

    Opencart? Okay, I've no experience with that and I must say that their mod_rewrite is WORSE than WordPress (only minor errors) and ZenCart.

    The RewriteBase directive is designed to UNDO a mod_alias redirection ... which you don't have. Therefore, it's used to alter the DocumentRoot (for mod_rewrite processing) to the 1521 subdirectory.

    The last RewriteRule (RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]) is asking Apache to match everything (or nothing) except the ? character (which is a reserved character so it'll NEVER be in a {REQUEST_URI} string). The redirection is to index.php with a query string key _route_ assigned the value obtained from the regex, the Last flag tells Apache to process the redirection and QSA says to append any pre-existing query string.

    To redirect /blog to /index.php?route=blog/category/home/index.php (within your 1521 subdirectory - RewriteBase, you know?), insert a simple RewriteRule before the generic redirection. The modified code would then be (corrected, of course - remove the red, add the blue code and remove my comments - they're explanations for you):

    Code:
    # SEO URL Settings
    RewriteEngine On
    # If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/ 
    
    RewriteBase /1521/
    RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
    RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
    
    # new redirection for blog
    RewriteRule ^blog$ index.php?route=blog/category/home/index.php [L]
    
    # generic redirection
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # not needed as existing files are excluded by the !-f above
    # RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
    RewriteRule ^(.*) index.php?_route_=$1 [L,QSA]
    # I hate the (.*) but you are trying to capture EVERYTHING (or NOTHING)
    # I normally specify .? and use %{REQUEST_URI} rather than $1
    # but I'm not sure how the RewriteBase would affect this
    Oh, yes, thank you for not taking offense and understanding my refusal to code for "script kiddies."

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great that works a treat! And I think I'm beginning to understand what you are talking about, though I see your link is now working so I'll have a look at your documentation at some point.

    I've removed the $ next to ^blog$ as I'd like both /blog and /blog/ to work, and removing that seems to have done the trick. Hopefully I won't regret that somewhere down the line.

    Thank you for your time, and I understand your frustration at script kiddies - no doubt they can be real time wasters.

    Cheers,
    Dan

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Dan,

    Quote Originally Posted by dantheman50_98 View Post
    Great that works a treat! And I think I'm beginning to understand what you are talking about, though I see your link is now working so I'll have a look at your documentation at some point.

    Great!

    I've removed the $ next to ^blog$ as I'd like both /blog and /blog/ to work, and removing that seems to have done the trick. Hopefully I won't regret that somewhere down the line.

    OMG! NOoooooo!

    All that will do for you (optional subdirectory level) is confuse visitors' browsers as to the directory level to look for relative linked files - UNLESS all your links are absolute.

    Please have a look around this board as I whine about "common errors" like this all the time!

    Thank you for your time, and I understand your frustration at script kiddies - no doubt they can be real time wasters.

    I don't mind wasting time helping members LEARN. It's the ones that want to be spoon-fed code without learning that annoy me. I believe that a webmaster must know what his/her code is doing to justify being a webmaster - using unknown code is both stupid (doesn't understand the problems their code - like the infamous EVERYTHING atom; (.*) - is generating) and lazy (I have a low tolerance for that, too).

    Cheers,
    Dan
    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  7. #7
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Woah, thanks for pointing that out - I'll scout about your posts for a better solution, or presumably I'll find the answer if I actually study your documentation on the subject, as promised. Thanks again for letting me know my error!

  8. #8
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Dan,

    No worries. As I said, it's a common error (then "webmasters" wonder why their relative links fail half the time). The part of the tutorial which discusses "lost supporting files" targets the redirection which changes directory level without making the change visible to visitors (actually, to their browsers). Making a trailing / optional does the same thing AND tells Apache to look for the DirectoryIndex of the DIRECTORY named the same as the file name. Okay, you can defeat that with Options MultiViews but that's another can of worms.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  9. #9
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi DK,

    Thanks, I'll get onto this in the morning (it's late at night here in the UK). For the time being I've done something you'll no doubt find appalling, but I've just copied the original line and added a slash:

    RewriteRule ^blog$ index.php?route=blog/category/home/index.php [L]
    RewriteRule ^blog/$ index.php?route=blog/category/home/index.php [L]

    I'll get onto a proper solution first thing tomorrow!

    Cheers,
    Dan

  10. #10
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Dan,

    Quote Originally Posted by dantheman50_98 View Post
    For the time being I've done something you'll no doubt find appalling, but I've just copied the original line and added a slash:

    RewriteRule ^blog$ index.php?route=blog/category/home/index.php [L]
    RewriteRule ^blog/$ index.php?route=blog/category/home/index.php [L]



    I'll get onto a proper solution first thing tomorrow!

    Cheers,
    Dan


    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  11. #11
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right I think I've got it:

    Code:
    RewriteRule ^blog/?$  index.php?route=blog/category/home/index.php [L]
    I've done this based on what you say here: "I satisfy both versions by making the leading slash optional, i.e., ^/?" and judging by the fact that the url now works with and without a slash it looks like it works.

    Thanks!
    Dan

  12. #12
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)


    Nope! You're not getting the message about the location visitors' browsers think they're at when requesting relative links from the script (not to mention that MultiViews comes into play). I'd say to either FIX blog as a directory with the trailing / (it will only matter in that it changes the directory level) OR FIX it as an extensionless file (where the redirection is to the same directory level so relative links will be fine).

    If you don't believe me (which, apparently, you don't), try linking to blog then to blog/ and see the difference in the presentation returned by index.php.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  13. #13
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe you, and I'm starting to understand what you're talking about...I'm a slow learner.

    So, what you're saying is it's an either/or situation? There must be a way to redirect the url with a trailing slash to the url without, so that people don't end up on an error page if that add it by accident...

    Cheers,
    Dan

  14. #14
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Dan,

    Quote Originally Posted by dantheman50_98 View Post
    I believe you, and I'm starting to understand what you're talking about...I'm a slow learner.


    So, what you're saying is it's an either/or situation? There must be a way to redirect the url with a trailing slash to the url without, so that people don't end up on an error page if that add it by accident...

    For me, it's not even either/or, it's NEVER use a trailing slash EXCEPT for a directory URI (and, even then, I prefer to specify the DirectoryIndex).

    Yes, remove the trailing / from non-directory URIs with a 301 redirection then get on to real business:

    [code]RewriteEngine On

    RewriteBase /1521/
    RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
    RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]

    # remove trailing /'s from non-directory URIs
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*)/$ $1 [R=301,L]

    # new redirection for blog
    RewriteRule ^blog$ index.php?route=blog/category/home/index.php [L]

    # generic redirection
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*) index.php?_route_=$1 [L,QSA]

    Cheers,
    Dan
    Actually, I prefer NOT to spoon-feed visitors like that (removing trailing /'s) and will redirect them to the 404 script which, after posting a message, sends then to a sitemap where they can click on the link without the trailing /. IMHO, it doesn't pay to dumb-down for the ID-ten-Ts out there (but don't lose visitors by not providing a route to the correct page).

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  15. #15
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That works perfectly, thank you. I fairly certain I understand what you've given me here, so I'm definitely learning!

  16. #16
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Hi Dan!

    Well, learning is what's important (for me as someone trying to give you the knowledge it took my blood, sweat and tears to accumulate). Of course, my knowledge is biased by my experience so take it for what it's worth. I may not always be right but I'm entitled to my opinion and try to explain WHY I have my opinions to help other avoid the same pitfalls I suffered along the way.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


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
  •