Url rewriting mod_rewrite - .htaccess

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.

Related

mod_rewrite select everything

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 -.

Very simple mod_rewrite help

I've got a directory called fb and a script inside called like.php. I'd like to have the get-id passed to the like-file using mod_rewrite.
mypage.com/fb/like.php?id=5 would be mypage.com/fb/like/5
My (not working) htaccess looks like this:
RewriteEngine on
RewriteRule /fb/like/([0-9]+) /fb/like.php?id=$1
Does anyone see what's wrong here?
Try removing the slash at the beginning of your match and replace patterns like so:
RewriteRule fb/like/([0-9]+) fb/like.php?id=$1
mypage.com/ is the domain name, so the string that gets matched is fb/like/5
Also consider using the carat at the start of your match string so that it will match fb but not fffb:
RewriteRule ^fb/like/([0-9]+) fb/like.php?id=$1
Here's a short guide to mod_rewrite I've found helpful.
Edit for your follow-up question:
To match mypage.com/something/fb/like/5, you can do this:
RewriteRule ^([^/]+)/fb/like/([0-9]+) $1/fb/like.php?id=$2
This saves the first directory as $1. [^/]+ means match one or more characters that are not a slash. Put this .htaccess file in the root directory of your domain.
Alternatively, you can use the second-to-last rule and put that .htaccess file in the "something" subdirectory. Hope that makes sense.
Or you can write a rule to match simply like.php/([0-9]+) so that it'll work no matter what the directory path looks like. You can go even more generic and make this apply to any PHP file, not just like.php. It really depends on how you want your site to work.

htaccess: redirect to new url problem

I would like to redirect any php page with optional parameter to a new clean url
eg, from: account.php?id=156 to newurl/account/156
I'm using the following redirectmatch
RedirectMatch ^/cmstut/([a-zA-Z0-9_]+)[\.php]?[\?]?[a-zA-Z_]?[=]?([0-9]+)?$ /cmstut/redirect/newurl/$1/$2/$3 [L]
but the result I get is it will redirect to newurl/account//?id=156
I thought it was funny when I read somewhere where htaccess and regular expression was compared to voodoo :) we'll now I understand why
I don't understand where your third subexpression is.
$1: ([a-zA-Z0-9_]+)
$2: ([0-9]+)
$3: MIA
May I recommend something more like this?
/cmstut/([A-Za-z0-9]+)\.php(\??id=([0-9]+))?
then you may have to use $3 to access the id number. you want the entire parameter to be optional, right?
I never used URL redirects in .htaccess before, but if it's plain regular expressions, this should work:
RedirectMatch ^/cmstut/([a-zA-Z0-9_]+)\.php(\?[a-zA-Z_]+=([0-9]+))?$ /cmstut/redirect/newurl/$1/$3 [L]
Since you weren't very specific in what you want, I took some guesses. This will redirect foo.php?bar=123 to newurl/foo/123 and will ignore bar.
Edit: Thinking about it, rewriting your regexp for you won't help you in the long term, and no one except you is likely to know exactly what you want. I think a better course of action is pointing you to a regexp guide. Here is one, and it's specifically targeted for mod_rewrite.
I finally found the solution. I did some more research and I used a different approach which I believe was even better then what i was using
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC]
RewriteRule ^([A-Za-z0-9]+)\.php$ /cmstut/redirect/newurl/account/%1? [R=302,L]

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] ?

.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