I know the basic rewrite rule in .htaccess file. but i can't find the meaning of highlighed ? and $:
RewriteRule ^([a-zA-Z])/?([a-zA-Z])?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&id=$3 [NC,L]
Formatted rule code:
RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&id=$3 [NC,L]
Can someone tell what they used here. I am trying myself but can't find this anywhere . any help would be very appreciated
This question is more of regex rather than a .htaccess one. Let me try to to answer:
? in regex means 0 or 1 match or to make something an optional match
$ is end anchor in regex that means the end of input.
Your rewrite rule appears to be making both action and id components optional and this rule allows for an optional trailing slash at the end of the pretty URI.
Related
I need to add Rewrite/Redirect statements in my .htaccess to redirect a specific url to a different .php page with masking. The goal is when someone hits:
www.mydomain.com/sandbox
i need it to go to:
server.mydomain.com/directoryname/phpprogram.php. But I need it to mask so the address bar will still show www.mydomain.com/sandbox
Appreciate any help with this. Thanks.
You can use mod-rewrite to maks your URLs.
The following rule in an htaccess file should do the trick :
RewriteEngine On
RewriteRule ^sandbox/?$ /directoryname/phpprogram.php [L]
This will internally map a request for example.com/sandbox to example.com/directoryname/phpprogram.php .
The /? in the regular expression pattern above matches the traling slash as an optional character so the rule will also match example.com/sandbox/ .
I have many redirects in the .htaccess file. Now, I need to create a rewrite rule for a URL based on it's querystring value..
http://www.mydomain.com/subdir/RentalDetails.aspx?RentalId=1072
any traffic to this url should go to...
http://www.mydomain.com/subdir/RentalDirectory.aspx
I have researched and found some good posts on the subject and came up with this..
#discontinued properties
RewriteCond %{REQUEST_URI} ^/RentalDetails\.aspx$
RewriteCond %{QUERY_STRING} ^RentalId=1072$
RewriteRule ^(.*)$ /subdir/RentalDirectory.aspx? [R=301,L]
Unfortunately, it is not working. Could someone please help me to figure out where I have gone wrong? Many thanks in advance!
I'd guess that the RewriteCond is wrong: the uri shouldn't look like
^/RentalDetails\.aspx$
[Edited -- I finally managed to get to the actual docs, and found the query string was a red herring. However, I missed the '^' at the start of the string as well, which may be causing this]
Since the ^ at the start of the expression means "the string must start here", and the $at the end means "the string ends here", your regex is too restrictive, and won't match what you need it to.
From the apache docs, the REQUEST_URI variable excludes the query string, so that's not the issue. However, you are trying to match a url of /subdir/RentalDetails.aspx with a regex that has to start with the word /RentalDetails
Try something like
# No ^ at the start, this will match RentalDetails.aspx in any location
/RentalDetails\.aspx$
Or
# This will only match in the /subdir/ directory.
^/subdir/RentalDetails\.aspx$
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.
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
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