.htaccess work with and without trailing slash - .htaccess

Using .htaccess, how can I get my website to be able to do this. Here is what I have already.
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)/(.*)$ sites/index.php?url=$1&page=$2
From this, I am able to be able to browse to:
http://domain.com/sites/example/
and that works totally fine.
What I want to be able to do is browse to
http://domain.com/sites/example
with no trailing slash, but it always gives back an error with page not found.
So far from Google, all of the results have failed me.
Thank you.

Try changing your regex to:
RewriteRule ^([^/]+)/(.*?)/?$ sites/index.php?url=$1&page=$2
The first (.*) is greedy, and will gobble up everything.

Yeah the sea of info in google can sometimes be a hinderance:
RewriteRule ^(.*)/(.*)/?$ sites/index.php?url=$1&page=$2
Note the /? in the rewriterule before the $, sorry I've read your question backwards...
edited answer:
RewriteRule ^(.*)/?(.*)$ sites/index.php?url=$1&page=$2
The different position of the ? might help as otherwise the greedy match of the first(.*) might glob to the end and try and shove that all into $1, for the specifics you'd be best turning on the apache rewrite log and looking at that if you can.

Related

RewriteRule not working asa expected .htaccess

Ok, so here's my RewriteRule:
RewriteRule ^books/book-([0-9]+)/(.*[^/])$/? authors/book-$1/$2 [R=301,L]
so if someone goes to www.example.com/books/book-5/some-book-title, they are redirected to www.example.com/authors/book-5/some-book-title. This bit works fine.
However, the problem I have is this:
www.example.com/books/book-5/some-book-title/still-more-stuff/still/more/stuff
it will forward to www.example.com/authors/book-5/some-book-title/still-more-stuff/still/more/stuff.
I want it to stop after www.example.com/books/book-5/some-title/ <- It should stop here!!!
It should not allow more parameters after the /some-title/ <- or it should disregard them.
How can I achieve this? Only grab the first parameter /some-title/, otherwise throw a 404? I thought that by putting the [^/] bit, it would stop when it found a forward /.
Thanks
Tweak your regex like this:
RewriteRule ^books/(book-\d+/[^/]+) authors/$1 [R=301,L,NE,NC]
Problem is actually presence of .* in your regex.

Awstats Redirection Using Mod_Rewrite and Issue with Colon Symbol

I have spent hours trying to get http://www.example.com/awstats to redirect to http://www.example.com/awstats/awstats.pl?config=example.com
The rewrite rule I used is:
RewriteRule ^https?://(www.)?(.*)/awstats/?$ /awstats/awstats.pl?config=$2 [QSA,L]
This doesn't work but it seems that it will work without the colon symbol.
I have tried using the HTML character code and escaping it but nothing I do seems to make any difference.
I have been testing it here: http://htaccess.madewithlove.be/
If I supply this value:
http//www.example.com/awstats
With this rule:
RewriteRule ^https?//(www.)?(.*)/awstats/?$ /awstats/awstats.pl?config=$2 [QSA,L]
Then it works but obviously I need it to process the colon symbol.
Please can someone tell me what I am dong wrong?
Thanks
Robin

Using .htaccess to style URL directory style

I have searched this question and looked around but can't seem to get this working in practice. This is my .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule /poker/(.*)/(.*)/$ /poker/?$1=$2
I am trying to get my page to work like this:
mysite.com/poker/page/home
But this just isn't working, I have used 3 different generators and tried typing it manually from tutorials but it is just returning a 404. Any idea's a greatly appreciated, it could be really obvious..
Thanks
You do not have a trailing slash in your example, yet your rule requires one. You can make the trailing slash optional:
RewriteEngine on
RewriteRule /poker/(.*)/(.*)/?$ /poker/?$1=$2
Note however, that a uri /poker/a/b/c/d/e/f/g/ is also a match here - a/b/c/d/e/f will match the first subpattern and g will match the second one, because (.*) is greedy. Be more specific if you wish to match only content between slashes - e.g. ([^/]*)
Well, there's really nothing wrong with the rules that you have if http://mysite.com/poker/?page=home resolves correctly. The only thing is that if this is in an htaccess file, the leading slash is removed from the URI when it's matched against in a RewriteRule, so you need to remove it from your regular expression (or maky it optional):
RewriteRule ^poker/(.+)/(.+)/?$ /poker/?$1=$2
And maybe make the groupings (.+) instead so that there is at least one character there.

agh - mod_rewrite remove '-xxx-XXX' from URL

bit of a nightmare. Clients sent out a big email with in links in them which have subsequently changed!
just need to change
forthcoming-events/event/skills-xxx-XXX
to
forthcoming-events/event/skills
so just removing the '-xxx-XXX'. Any help majorly appreciated as struggling to find a solution.
I'd have thought that something like:
RewriteRule \A/forthcoming-events/event/skills-xxx-XXX/*$ /forthcoming-events/event/skills/$1 [L]
...would do the trick. (That said, I've presumed that the "skills-..." bit is a directory.
RewriteRule ^(.*)-xxx-XXX$ $1 [R=302,NC]
the above works to match the rewrite the URLs!
cheers...

How to make a catch-all handler in an .htaccess file?

I want to create a rule at the end of an .htaccess file that catches everything that failed to match up until then.
How can I do that?
P.S. I've already tried everything :) Actually, I haven't, but it sure seems like it!
Update: Some people are replying with RewriteRule ^.*$ or an equivalent. This does not work! It will match everything including the other 'good' URLs.
There are actually some good answers here already, but you have responded with...
But then this matches everything including the good stuff.
This is because you aren't telling mod_rewrite to stop processing on a match. To do this, use the "L" tag after each rule, which tells mod_rewrite that "If this rule is matched, stop processing any further rules".
RewriteRule ^RSS/([^/\.]+)/?$ rss.php?Page=$1 [L]
You need to put this after EACH rule. Then, when you put the catch all at the end, it will only be hit if no other rule has been matched.
NOTE: if you are ALSO serving up resources that are not rewritten, like CSS, images, javascript files - you are honestly better off not catching all as you wouldn't want to rewrite their locations.
It sounds like you want to look at the RewriteRule directive. Quie possibly something similar to the following:
RewriteRule ^/(.+) {target}
This will match at the root URL (/) and redirect it to the {target} url.
RewriteRule .* page.php [L] ?

Resources