htaccess is not working - .htaccess

I am trying to write an htaccess rewrite rule. But it is not redirecting,
This is my present rule
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ question.php?qkey=$1
that will show a url like sitename/questionkey and redirect it perfectly.
Now Iam trying to show a url like sitename/questioncatagory/questiontititle
Iam trying to use the following rule, but it is not working
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/^([a-zA-Z0-9]+)/^([a-zA-Z0-9]+)$ question.php?qkey=$1

First, it's probably better for clarity and maintenance to replace your ([a-zA-Z0-9]) with ([\w]+)
Secondly, your new rule doesn't work because of the caret ^ character. In the beginning, it indicates 'match beginning of the line', which surely doesn't apply 3 times total in the regex. Remove the two later ^ carets (and then make use of your additional captured groups somewhere with $1, $2, et c).
Lastly, you probably don't need to match the end of the line with the $ character. This is unfriendly to many URLs, for example ones with a trailing slash.
RewriteEngine On
RewriteRule ^([\w]+)/([\w]+)/([\w]+) question.php?qkey=$1&cat=$2&qtitle=$3

Related

.htaccess basic mod rewrite

I am only just starting to learn how to rewrite urls with the .htaccess file.
How would I change:
http://www.url.net/games/game_one.php
into this:
http://www.url.net/games/game-one/
This is what I have been trying
RewriteRule ^/games/game-one.php ^/games/game-one/ [NC,L]
If you want people to use /games/game-one/ explicitly, you have to rewrite so that it requests /game/game-one.php. So the opposite way around than you have it in your question.
RewriteEngine On
RewriteRule ^games/game-one/$ /games/game-one.php
If you want to rewrite other URL's too, then you'd need to use a technique similar to the prior answer.
Try this:
RewriteRule ^(/games/game-one)\.php $1/
What that says is match anything starting with /games/game-one and remember the first part of that match, then replace it with the first part (capturing group in regex speak), and a slash character. Note that to match a period character you must precede it with a \ since . is a special character that means "any character" (at least if you care to avoid matching any character).

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

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.

Match "full stop" in mod rewrite

At the moment I am just matching numbers, letters, dashes and underscores in my .htaccess file:
RewriteRule ^([A-Za-z0-9-_]+)/?$ index.php?folder=$1
I also want to match full stops in the string. I don't want to use:
(.*)
I have tried:
([.A-Za-z0-9-_]+)
([\.A-Za-z0-9-_]+)
([\\.A-Za-z0-9-_]+)
([A-Za-z0-9-_\.]+)
None of which seem to work.... how can I escape the full stop so it matches a full stop!
---------- Additional information ----------------
As an example:
mydomain.com/groups/green/ should go to index.php?folder=green
In addition I am also re-writing subdomains over the top of this (I think this is causing the complication)...
anotherdomain.com should map to index.php?folder=anotherdomain.com
I have succesfully re-written the subdomain with the following rule:
# external group domain name
RewriteCond %{ENV:Rewrite-Done} !^Yes$
## exclude requests from myhost.com
RewriteCond %{HTTP_HOST} !^www\.myhost\.com
## allowed list of domain masking domains
RewriteCond %{HTTP_HOST} ^(anotherdomain.com|extra.domain.com|external.otherdomain.com)
RewriteRule (.*) /groups/%1/$1
I think this is where the complication lies.
---------------- Solution ----------------------
Despite not finding a solution to the exact problem above, I have worked around it by changing the first re-direct (which maps the external domains) from:
RewriteRule (.*) /groups/%1/$1
to:
RewriteRule (.*) /groups/external/$1&external_domain=%1
The second re-write (on the folder) can then interpret the "external domain" variable instead of the folder.
Your first option is the simplest and is correct. Inside square brackets . has no special meaning, so you include it verbatim without any special escaping needed.
Actually there is a small problem with the second dash in 0-9-_. If you want a dash inside square brackets you should place it at the beginning of the character class. Otherwise it will have its special meaning of defining a character range:
([-.A-Za-z0-9_]+)
If that doesn't work there is something else wrong with your RewriteRule. For instance, if this is a global rule rather than per-directory (no RewriteBase) then URLs will begin with a slash /.

.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