RewriteRule not working asa expected .htaccess - .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.

Related

htaccess to add ?query=string to specific url

I'm pulling my hair out trying to get a .htaccess rewrite rule to work. I'm sure this should be easy but I just can't get it right!
I need to add ?query=string to a specific URL pattern so:
www.example.com/downloads/file
Becomes:
www.example.com/downloads/file?query=string
The best I can come up with is:
RewriteRule ^downloads/(.*) downloads/$1?query=string
But it's not working. Where am I going wrong?
What you have looks OK, except you might need to include the L flag to prevent further rewritting:
RewriteRule ^downloads/(.*) downloads/$1?query=string [L]
And if this is intended to be an external redirect then you'll need to make the substitution absolute or root-relative and include the R flag:
RewriteRule ^downloads/(.*) /downloads/$1?query=string [R=302,L]

.htaccess work with and without trailing slash

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.

htaccess rewrite, url doesn't work when i add a / at the end

I have got a problem that seems to me something simple but i'm new to htaccess and can't find the solution.
i've got the following line in my htaccess (root map)
RewriteRule ^page1/([a-zA-Z0-9_-]+)/$ page1.php?name=$1
RewriteRule ^page1/([a-zA-Z0-9_-]+)$ page1.php?name=$1
When i enter the the following url it works without a problem
www.myexample.com/page1/variable
the strange thing happens when I add a / at the end. Then the page can't get the GET value out of the URL.
Thanks for your time and help!
Get rid of the ending /$ sign on the first rule
RewriteRule ^page1/([a-zA-Z0-9_-]+) page1.php?name=$1
Or you can continue to capture data
RewriteRule ^page1/([a-zA-Z0-9_-]+)/(.*)$ page1.php?name=$1
Ultimately if you want to keep capturing more data with "/" as a delimiter, I suggest doing
RewriteRule ^page1/(.*)$ page1.php?url=$1
Then use the server side script to determine what to do.

.htaccess file url rewrite keeps going to the wrong page

I am trying to get the follow urls to work
www.urlname.com/team/1-Test
www.urlname.com/team/1-Test/members
RewriteRule ^team/([^-]+)-([^&]+)$ index.php?p=teamprofile&team_name=$2&team_id=$1
RewriteRule ^team/([^-]+)-([^&]+)/members$ index.php?p=teammembers&team_name=$1&team_id=$2
but when i try the link with /members init it goes to the other page?
can someone help me please
Thanks
[^-] and [^&] includes the / so /members is included with that. you could either add / to your negation character groups like [^-/] and [^&/] so it doesn't match / or move the bottom one up and add [L] after it to tell apache this is the [L]ast rule to check if it matches.
The trouble is, your second rule is being satisfied by the first rule. You could simply switch them around and it will work:
RewriteEngine On
RewriteRule ^team/([^-]+)-([^&]+)/members$ index.php?p=teammembers&team_name=$1&team_id=$2
RewriteRule ^team/([^-]+)-([^&]+)$ index.php?p=teamprofile&team_name=$2&team_id=$1
Although, a slight change in the first rule will also address the problem:
RewriteEngine On
RewriteRule ^team/([^-]+)-([^/]+)[/]?$ index.php?p=teamprofile&team_name=$1&team_id=$2 [L]
RewriteRule ^team/([^-]+)-([^/]+)/members[/]?$ index.php?p=teammembers&team_name=$1&team_id=$2 [L]
Note, I changed the match string in the first rule from ([^&]+) to ([^/]+) - that way the forward slash isn't included in the match in cases like mydomain.com/team/1-2/. The [/]? rule at the end is an optional match for that trailing forward slash. I've likewise added one to the end of the members rule as well, now it works like this:
mydomain.com/team/1-2/ - goes to index.php?p=teamprofile&team_name=1&team_id=2
mydomain.com/team/1-2 - goes to index.php?p=teamprofile&team_name=1&team_id=2
mydomain.com/team/1-2/members - goes to index.php?p=teammembers&team_name=1&team_id=2
mydomain.com/team/1-2/members/ - goes to index.php?p=teammembers&team_name=1&team_id=2

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