I am working on a site which will have two levels the URL reaches
My objective is to have clean URL's like this...
http://domain.com/username/dosomething
My ugly URL's currently look like this...
http://domain.com/index.php?page=username&command=dosomething
My attempt was this
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ index.php?page=$1&command=$2
RewriteRule ^([a-zA-Z0-9]+)/$1/$2 index.php?page=$1&command=$2
You're not using your backreferences correctly in the first part. Backreferences are parenthesised expressions that will then get filled into $1, $2 et al. in the second part of the rule. e.g.:
RewriteRule ^([^/]+)/([^/]+)$ /index.php?page=$1&command=$2
These parenthesized expressions match one or more non-/ characters and fill them into $1 and $2 respectively.
Your references are just referencing your entire URL so what you are telling it to give you is
index.php?page={entire URL}&command={null}
You need to setup the URL and only use parenthesis around the variables page and command. So
^domain.com/([username]+)/(dosomething)
then rewrite with:
http://domain.com/index.php?page=$1&command=$2
provided that your page and command variables are username and dosomething respectively
Related
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.
RewriteRule admin$ admin.php [R,NC,L]
Rewrites www.anything.at/all/that/ends/in/admin to www.anything.at/admin.php
I want to rewrite a URL like:
http://domainname.com/all-studio-methods
To:
http://domainname.com/review.php?id=25&cas=all-studio-methods
My .htaccess file currently looks like the following:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([^/\.]*)$ review.php?id=$1&cas=$2 [L]
But it is not working properly. What am I doing wrong?
As noted in the comments, you have this rule:
RewriteRule ^([^/\.]*)$ review.php?id=$1&cas=$2 [L]
The $1 and $2 are backreferences that are replaced with matched groupings in your reqular expression, ^([^/\.]*)$ which only has 1 grouping, the entire match. So $2 will always be blank since you don't have another grouping. This also means that $1 will be the entire match (e.g. all-studio-methods) and you're going to get a URI like this:
review.php?id=all-studio-methods&cas=
Which is obvioiusly not what you want. The comments ask where the id=25 comes from. It's not coming from the URI, /all-studio-methods. So in order to rewrite to id=25, it's got to be in the URI somewhere, for example:
http://domainname.com/25-all-studio-methods
Then you'd have a rule like:
RewriteRule ^([0-9]+)-([^/\.]*)$ review.php?id=$1&cas=$2 [L]
If you really don't want the 25- in the URI, you'll need to rewrite the php code in review.php so that it doesn't take an id. It would need to fetch the ID internally from the database given a cas.
Or, you could create a rewrite map in order to map cas to an id. It's going to be pretty much the same thing, you're writting code to do it in either case.
I have a question about htaccess rewriting rule.
Is it possible to convert this url:
www.site.com/en/test
In something like this:
www.site.com/test.php?language=en
I tried to use this RewriteRule
RewriteRule ^([a-z][a-z])/(.*)$ $1.php?language=$2
But in some online testing tool the result URL was:
www.site.com/en.php?language=test
Thank you in advance.
Mat
The reason for this confusion is that you've reversed the matches. When you use references, the first reference in the match is always $1, the second is $2 and so on.
Here's what you did, looking only at the parentheses:
([a-z][a-z]) = first match, places "en" in $1
(.*) = second match, places "test" in $2
So you simply need to reverse them in your output, like this:
RewriteRule ^([a-z][a-z])/(.*)$ $2.php?language=$1
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
I'm trying to redirect this
hhttp://www.website.net/forum/index.php?showtopic=12345
to
hhttp://www.website.ORG/forum/t12345
12345 being the dynamic topic ID
I also need any information to be stripped away if it is found after the topic ID, for example
hhttp://www.website.net/forum/index.php?showtopic=12345&view=getlastpost
I want &view=getlastpost or any similar that may appear after the ID number to be get rid of.
I've tried
RewriteCond %{QUERY_STRING} ^(([^&]&))showtopic=([^&]+)&?(.*)?$
RewriteRule ^index.php$ http://www.website.org/forum/t%3?%1%4/ [L,R=301]
but it didn't work. I get trash in the URL.
hhttp://www.website.org/forum/index.php?showtopic=29294&view=getlastpost (when that link is clicked - the result is hhttp://www.website.net/forum/t29294?view=getlastpost/)
hhttp://www.website.org/forum/index.php?showtopic=29029 (when that link is clicked - the result is hhttp://www.website.net/forum/t29029?/).
How can I clear it out?
$2 implies there are two bracketed areas, but I only see one in your rule, so changed that to $1.
Also your URL starts /forum/ so need to include that in the rule.
And the . in index.php needs to be escaped if you don't want it treated as a regex special character.
And if you want to ditch anything after the showtopic=1234 then just remove the $ that indicates the end of the string
RewriteRule ^forum/index\.php?showtopic=([0-9]*) http://www.website.org/forum/t$1/ [L,R=301]