.htaccess file url rewrite keeps going to the wrong page - .htaccess

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

Related

.htaccess remove duplicate part of a URL

some bug in my online catalog SEF URL generation created a situation where some category slug is added to the URL twice. to fix this i would like to use .htaccess to remove the this part of the URL and have the whole URL shift up one level including URL's which also include a product under them.
the duplicate occurrence is of the sub-category painting-tools under the category tools
so the wrong URL looks like this:
/store/items/catalog/tools/painting-tools/painting-tools
or
/store/items/catalog/tools/painting-tools/painting-tools/{some product}
instead of being the correct URL like this:
/store/items/catalog/tools/painting-tools
or
/store/items/catalog/tools/painting-tools/{some product}
and the URLs
/store/items/catalog/tools/painting-tools/
or
/store/items/catalog/tools/painting-tools/{some product}
should actually be like this
/store/items/catalog/tools
or
/store/items/catalog/tools/{some product}
tried using this rule, but its not working
RewriteRule ^/painting-tools/painting-tools/(.*) /painting-tools/$1 [QSA]
i think the ^ part is not correct since its not the beginning of the URL.
How can i fix it ?
thanks
You can use this back-reference based redirect rule as very first rule in your .htaccess:
RewriteRule ^(.+?/([^/]+))/\2(/.*)?$ $1/$3 [NE,L,R=302]
RewriteRule ^(.+?/tools)/painting-tools(?:/.*)?$ $1/ [L,NC,R=302]
It is capturing repeat value after initial part and grouping it in this sub-pattern ([^/]+). Later in the regex it is using back-reference \2 to make sure same captured value is repeated.
Make sure you have proper RewriteBase defined to either / or /store/ wherever your htaccess is located.
Update: As per discussion below:
RewriteRule ^(.+?/tools)/painting-tools2(/.*)?$ $1$2 [L,NC,R=302]
This will remove /painting-tools2 from URLs.

How to write this .htaccess rewrite rule

I am setting up a MVC style routing system using mod rewrite within an .htaccess file (and some php parsing too.)
I need to be able to direct different URLs to different php files that will be used as controllers. (index.php, admin.php, etc...)
I have found and edited a rewrite rule that does this well by looking at the first word after the first slash:
RewriteCond %{REQUEST_URI} ^/stats(.*)
RewriteRule ^(.*)$ /hello.php/$1 [L]
However, my problem is I want it to rewrite based on the 2nd word, not the first. I want the first word to be a username. So I want this:
http://www.samplesite.com/username/admin to redirect to admin.php
instead of:
http://www.samplesite.com/admin
I think I just need to edit the rewrite rule slightly with a 'anything can be here' type variable, but I'm unsure how to do that.
I guess you can prefix [^/]+/ to match and ignore that username/
RewriteCond %{REQUEST_URI} ^/[^/]+/stats(.*)
RewriteRule ^[^/]+/(.*)$ /hello.php/$1 [L]
then http://www.samplesite.com/username/statsadmin will be redirecte to http://www.samplesite.com/hello.php/statsadmin (or so, I do not know the .htaccess file)
To answer your question, "an anything can be here type variable" would be something like a full-stop . - it means "any character". Also the asterisk * means "zero or more of the preceding character or parenthesized grouped characters".
But I don't think you need that...If your matching url will always end in "admin" then you can use the dollar sign $ to match the end of the string.
Rewrit­eRule admin$ admin.php [R,NC,L]
Rewrites www.anything.at/all/that/ends/in/admin to www.anything.at/admin.php

Mod_Rewrite to /subdirectory and /subdirectory/query

I'm having a difficult time getting into using mod_rewrite. I've been at this for about an hour googling stuff but nothing quite seems to work. What I want to do is change
example.com/species.php into example.com/species
and also
example.com/species.php?name=frog into example.com/species/frog.
Using
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^species/(.*)$ /species.php?name=$1
I can get example.com/species.php?name=frog to display as example.com/species/frog, and with
RewriteRule ^species/ /species.php
I can get example.com/species.php to display as example.com/species/, but I can't get both of them to work at the same time.
Also, example.com/species with no trailing slash always comes up as a 404.
I've considered just making a /species/ directory to catch any problems but I'd rather just have a few rules for one species.php file. Any help would gladly be appreciated!
Edit (because I can't answer my own question for 8 more hours):
I seem to have fixed both of my problems. I changed my .htaccess to:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^species/(.*)$ /species.php?name=$1
RewriteRule ^species/?$ /species.php
The second RewriteRule successfully redirects example.com/species to example.com/species.php while leaving the other RewriteRule working at the same time.
However, if I typed in example.com/species/ with a trailing slash, it was being read as example.com/species.php?name= and would throw an error because no name was submitted, so I just added
if(isset($_GET['name']) && empty($_GET['name'])) {header('location: http://example.com/species');}
so that if I used example.com/species/ it would redirect to /species and work as desired.
If you change the * (match zero or more) to a + (match one or more) in your first RewriteRule then you should stop seeing species.php?name= if a trailing slash is used.
This is because the + will require that something appears after the slash, otherwise the rule will not match. Then your second RewriteRule will match because it ends with an optional slash, but will not add the name= query string to the target URL.
You may also want to add the [L] flag (last) after the first rule, because you don't need the second rule to execute if the first rule matches. (Note that this will not stop the RewriteCond and RewriteRule tests being run on the resulting redirect URL, which will have to go through the .htaccess file just like any other request.)
See the Reference Documentation for mod_rewrite in Apache 2.4 (or see the docs for the version of Apache you're actually using).

Problem with .htaccess and RewriteRule

I have url's like games/xbox/2
2 being the page number I need the url rewritten. This is what I'm using:
RewriteRule games/(.*?)/$ games/consoles.php?console=$1
RewriteRule games/(.+?)/(.+?)/$ games/consoles.php?console=$1&page=$2
The first rule works fine but the second is returning consoles.php as $1 instead of xbox
RewriteRule games/([A-Za-z0-9]+)/?$ games/consoles.php?console=$1
RewriteRule games/([A-Za-z0-9]+)/([0-9]+)/?$ games/consoles.php?console=$1&page=$2
Using (.*?) would match even the / character so xbox/2 is treated as a whole
Try something like:
RewriteRule games/([^/]+)/([^/]+)/?$ games/consoles.php?console=$1&page=$2 [L]
RewriteRule games/([^/]+)/?$ games/consoles.php?console=$1 [L]
I first put your most specific rule first - that way you don't do a general match, then a later more specific match mangles that general rewrite.
I also specified the [L] flag to signify that you want the engine to stop looking for more matches at this point. Re-ordering the rules is redundant in this case because of the [L] flag, but it's a good practice to get into.
I also changed the expressions slightly. Rather than using ([A-Za-z0-9]+) like the previous poster said, I changed it to ([^/]+) because that will match everything but a slash, so you can have weird console or game names. If you want to make it more specific feel free to, but this way provides the most general use-case.

.htaccess rewritecond rewrite case level 2 folder=info

I wanted to set .htaccess to rewrite:
example.com/bla/info/
to
example.com/info/index.php?q=bla
example.com/bla/info/txt/
to
example.com/info/index.php?q=bla&t=txt
I wanted the browser still to display:
example.com/bla/info/txt/
I didn't want rewrite in the level 2 rewrites, like:
example.com/bla/xxx/
or
example.com/ccc/zzz/aaa/
but
example.com/silly/info/
would work as well as
example.com/strange/info/mytxt/
Did this make sense?
Any help?
If you start your pattern with ^ and end it with $, the rule will only apply if the whole string matches.
RewriteRule ^/bla/info/$ /info/index.php?q=bla
RewriteRule ^/bla/info/txt/$ /info/index.php?q=bla&t=txt
If you use do not use the [R] option, the URL shown in the browser will be whatever the user entered.
Are you trying to make this general-purpose? If so, you can use regex-type matching and variable substitution. To make 'bla' match any string up to the next slash (/), use
([^/]+)
in your pattern and reference it with $1 in your substitution.
RewriteRule ^/([^/]+)/info/$ /info/index.php?q=$1
RewriteRule ^/([^/]+)/info/([^/]+)/$ /info/index.php?q=$1&t=$2
I recommend the Apache web pages about mod_rewrite for more information.
[Edit. Fixed the rules to not include the host name in the pattern, as the original poster figured out.]
--
bmb
you got me in the right track.
i ended up with something like this:
RewriteRule ^([^/\.]+)/info/?$ example/info/index.php?q=$1 [L]
thanks a lot

Resources