mod_rewrite select everything - .htaccess

I have a problem with URL rewriting. This is the rule
RewriteRule ^(.*)-(.*)-(.*)\.html$ file.php?name=test&a=$1&b=$2&c=$3
The rule work's fine on a link like this:
test-11-abc.html
But I have a problem if the third variable have a dash like:
test-11-ab-c.html
The rewrite doesn't work.

Try this:
RewriteRule ^(.*?)-(.*?)-(.*)\.html$ file.php?name=test&a=$1&b=$2&c=$3
You need to make the first 2 matches non-greedy, because they're gobbling up the first -.

Related

.htaccess not working as expected in sub-folder

I have this rewrite rule placed in /dashboard/.htaccess [dashboard is actually a folder]:
RewriteRule ^([^/]*)$ index.php?mode=$1 [L]
My structure is index.php?mode=support, even though, $_SERVER['QUERY_STRING'] outputs this:
mode=index.php
Example: site.com/dashboard/index.php?mode=support should be site.com/dashboard/support
So , how can I make it parse the param value, and not the file itself.
Managed to solve it while doing more research on regular expressions.
RewriteRule ^([a-z]+)$ index.php?mode=$1 [L,QSA]
Thi solved my problem, preferred plus instead asterisk because it tells the engine to repeat it zero or more times. (when i'm on index.php , query string is empty as needed)
Your rule is matching anything that starts with not a slash and doesnt contain a slash anywhere when your actual path is
/dashboard/support
to get the folder you actually want you need a base on there like this
RewriteBase /dashboard/
If that is placed above the rule, Then your redirect should be ok

.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

Url rewriting mod_rewrite

I'm using this tester for url mod_rewrite test:
http://martinmelin.se/rewrite-rule-tester/
When I write something like this:
RewriteRule ^x/([a-z]+)/([0-9]+)/$ x.php?x=$2
Even though I have entered url :
x/p/6/
It get renamed to
x.php?x=p
and. What I want is
x.php?x=6
I'm new with mod rewrite, so anything would be useful.
I'm not a pro in mod_rewrite myself, but I think there is something wrong with that tool.
for example, if you change x.php?x=$2 to x.php?$1=$2, $2 will be parsed correctly.
And then, CMIIW but the regex looks fine. Test the regex here or here.
And Have you ever actually tried the RewriteRule yourself?
RewriteRule ^x/([a-z\/]+?)/([0-9]+)/$ x.php?x=$2
This adds all / characters to the first regex block. The ? makes the search non-greedy.

.htaccess passing domain name

I am trying to Rewrite path in .htaccess
=-=-=-=-=-EDIT=-=-=
Hi I may not have expressed my problem clearly.
So here's a live example of my problem.
"http://fames.in/site/bollywoodhungama.com" - works fine
"http://fames.in/site/bollywoodhungama.com/2" - error
The format of the url is:
http://fames.in/site/(site name)/(page number)
I use the following codes in .htaccess
RewriteRule ^site/(.*)$ sitelist.php?q=$1&page=1
RewriteRule ^site/(.*)/([0-9]+)$ sitelist.php?q=$1&page=$2
the first line works fine.
In the second line of the code htaccess passess the whole parameters to 'q'. taking above url it is passed as "/sitelist.php?q=bollywoodhungama.com/2" . I need it to pass the 'q' and 'page' separately. Like "/sitelist.php?q=bollywoodhungama.com&page=2"
The problem is that the first rule is always matching. The .* is greedy so the /2 is part of the match. What you need is the change the match to 'anything but slash' like so:
RewriteRule ^site/([^/]*)$ sitelist.php?q=$1&page=1
So the second domain will fail to match, and then the second rule will match.
Not sue if you need more rules applied after this rule of not, but you may want to add 'last rule flag' [L] to the end of you rule.

.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