I want to create my url structure like::
facebook i.e. facebook.com/?pageid=122
For which I am using htaccess mod rewrite as:
RewriteRule ^([a-zA-Z_\-]+)/?([a-zA-Z0-9\-=&_#]*)$ /$1.php?$2 [QSA,L]
so I may translate pages like site.com/home/?pageid=22 into site.com/home.php?pageid=22
The code above works fine except that if I try to add dot like
RewriteRule ^([a-zA-Z_\-]+)/?([a-zA-Z0-9\-=&_#\.]*)$ /$1.php?$2 [QSA,L]
The .htaccess breaks. I need dot so I may pass emails too i.e.
site.com/home/?email=sohaib.dmc#gmail.com
Please help
Try to remove the backslash before the dot. Since it's not considered as a special character inside brackets in a POSIX regular expression.
You need scape the question mark because it's a special character:
RewriteRule ^([a-zA-Z_\-]+)/\?([a-zA-Z0-9\-=&_#\.]*)$ /$1.php?$2 [QSA,L]
The question mark makes the preceding token in the regular expression optional. E.g.: colou?r matches both colour and color.
http://www.regular-expressions.info/optional.html
Related
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
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).
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 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.
Iam trying to redirect the url
mysite/love/this-is-another-question-about-lurv/29481751ffe886a64f9b0c9a
to
mysite/question.php?qkey=29481751ffe886a64f9b0c9a.
Iam using the following rewrite rule, but it is not working
RewriteEngine On
RewriteRule ([\w]+)/([\w]+)/^([\w]+) question.php?cat=$1&qtitle=$2&qkey=$3
If I recall, a dash character isn't considered a word character, try:
RewriteRule ([\w-]+)/([\w-]+)/^([\w-]+) question.php?cat=$1&qtitle=$2&qkey=$3
also I'm not sure what function that caret is playing in the last part. Outside of a [] it's normally used to indicate the start of a line, if that's the case here, than it would necessarily fail.
in which case:
RewriteRule ([\w-]+)/([\w-]+)/([\w-]+) question.php?cat=$1&qtitle=$2&qkey=$3
may perhaps be a working solution. I'd double check that - is a word character.