Link to home
Start Free TrialLog in
Avatar of MarFarMa
MarFarMa

asked on

need trailing slash fix for my custom htaccess config

This is my htaccess file contents (swapped out some-directory from the real value).  It works, except it doesn't handle a missing trailing slash.  

The guy who shared it with me knows the trailing slass problem exists -- but hasn't gotten around to fixing it.  It irritates me, so I thought I'd fix it on my own.  But my research into the subject of writing or fixing htaccess files has only proven to me that I need help!  

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/some-directory/$1 [R=301,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /some-directory/index.php?page=$1&seed=$2&keyword=$3 [L]

Can you tell me how ot fix it?

Thanks,

Pauli
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

It looks like the second rule already accounts for it.  Does it work as expected?  The first rule should look like this:

RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]/?$
Avatar of MarFarMa
MarFarMa

ASKER

If you try to go to http://www.mysite.com/some-directory you get a "not found" error, if you add the trailing slash, it works.
What is the purpose of the rewrite?

As it is now:

1) If the URL matches ^(.*)$ (which matches anything, so will be true for EVERY URL), and
2) The %{REQUEST_URI} matches a forward-slash, followed by any number of characters not including a period, followed by one character that is not a forward slash, THEN
3) Rewrite the entire URL to http://www.mysite.com/some-directory/TheOriginalRequest

RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/some-directory/$1 [R=301,L]

That means the rule will be matched for /some-directory, but not for /some-directory/.  In the first case, the rewrite would create http://www.mysite.com/some-directory/some-directory.
It allows a set of pages with relative links to root in the given subdirectory.  

So, if you have a small website with relative links to elements (images, etc.) and pages, that might otherwise be installed in the root of the domain, to be installed in the directory 'some-directory' and the links all resolve relative to that directory.  For example:

index.php ==> mysite.com/some-directory/index.php
contact.php ==> mysite.com/some-directory/contact.php
images/some-image.jpg ==> mysite.com/some-directory/images/some-image.jpg

etc.
Just occurred to me -- the htaccess file lives in  my-site/some-directory

I probably need one that lives in mysite.com's root directory that solves the trailing slash problem for some-directory, relative to the domain root.

If so, would apache read the htaccess rules in the domain root, add the slash, then read the htaccess file in the directory, and then root those files in that directory?  

Or do I have to accept this flaw (not correcting for a missing trailing slash on some-directory) to enable the behavior I want in some-directory?

I think perhaps you're using a sledgehammer to drive a thumbtack.  :)  Take a look here:

http://www.w3schools.com/tags/tag_base.asp

If you create an include file with just the base tag, and include it in all the files belonging to your mini-app, you should be good to go without having to bother with web server configuration.
Holy Bulls Eye, Batman!  I never heard of that tag!

OK - so what's the standard htaccess for my domain root directory that will fix the no trailing slash problem?  Just the first two lines of the four I posted?

SOLUTION
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I've been at a loss to describe the additional intent of this setup -- here's a quote that gets at it in a better fashion than I've been able to describe:

"Can I use dynamic keyword insertion on my display url to increase CTR? (E.g. www.example.com/dynamic-keyword)
Yes, but be aware that if your display url does not actually work as a real url, then your adverts may be disabled after a manual review. Use .htaccess or similar methods to rewrite the display url to your real landing page if you want to try this tactic."

Basiically it's to allow dynamic keyword insertion to create urls that will resolve - as well as to provide a default url where the site will be rooted.  page is a parameter, seed is a default value keyword, and keyword is the dynamically inserted keyword.  

(boy this has been an education oh url rewriting!)
What you describe is a fairly common use for mod_rewrite.  I use it myself to give articles and category pages user-friendly names.  They show up better on SERPs, and are easier to remember.

If you provide an example of what you want rewritten, and how it should appear after the rewrite, I can try to help you with that.
I provided a good explanation and proper examples, and believe the correct answer is in here somewhere.  :)

I recommend 21076079 and/or 21078264.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial