.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:

# 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

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

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

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):

# 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]

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

# generic redirection
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
[COLOR="#FF0000"]# not needed as existing files are excluded by the !-f above
# RewriteCond %{REQUEST_URI} !.*\\.(ico|gif|jpg|jpeg|png|js|css)[/COLOR]
RewriteRule ^([COLOR="#0000FF"].*[/COLOR]) index.php?_route_=$1 [L,QSA]
[COLOR="#0000FF"]# 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[/COLOR]

Oh, yes, thank you for not taking offense and understanding my refusal to code for “script kiddies.”

Regards,

DK

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

Dan,

Regards,

DK

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!

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

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

Dan,

Regards,

DK

Right I think I’ve got it:

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

:nono:

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

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

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

That works perfectly, thank you. I fairly certain I understand what you’ve given me here, so I’m definitely learning!

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