I have a problem in rewriting my dynamic url pattern in localhost.
here is my url: http://localhost/realtor/?module=property&action=popular-residential-buy
and i want the url to be http://localhost/realtor/property/popular-residential-buy
I have done so far in my .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule type (.*)-(.*)$ /?module=$1&action=$2
but it's not working.
Your existing regular expression gobbles up too much of your URL. You'll end up with something other than what you want in $1 and $2 (probably realtor/property/popular-residential in $1, and just buy in $2, due to the first .* greedily matching as much as it can before back tracking one character at a time to find a match).
Based on the URL you provided, it looks like your regular expression should be something like ^realtor/([^/]*)/([^/]*)/?$. That will give you what you want in $1 and $2, and it should be a quicker regular expression AFAIK.
Past that, make sure that you do have the RewriteEngine properly configured on your particular server.
For Apache, you'll need to enable mod_rewrite. Look in your httpd.conf for LoadModule rewrite_module modules/mod_rewrite.so, and make sure it is uncommented.
IIS is a bigger can of worms -- let me know if that is what you are using, and I will write a more complete answer.
Related
Okay so I am trying to make it so that if people go to /?char=USERNAME it would show the contents of /game/CharWidget.swf?login=USERNAME. This is my code so far:
RewriteEngine on
RewriteCond %{QUERY_STRING} char=(.*)
RewriteRule ^index.php?char=(.*) /game/CharWidget.swf?login=%1
This makes the url server side as /game/CharWidget.swf but doesn't carry the ?char=username and make it ?login=username so it wont show what I want it to show.
Edit; If it's easier doing /char/USERNAME to /game/CharWidget.swf?login=USERNAME i wouldnt mind doing that if someone could give me the code for it.
The query string is not visible to RewriteRules, so ^index.php?char=(.*) will never match. (Except that, since you haven't escaped . or ?, it will match e.g. indexZphchar=foo, which is probably not what you want.)
Also, if the user visits /?char=USERNAME, what the RewriteRule would normally see is just /; no index.php. Finally, if this is in an .htaccess file, you'll generally also need a RewriteBase directive.
Putting all those fixes together, something like this should work:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^char=(.*)$
RewriteRule ^/?(index\.php)?$ /game/CharWidget.swf?login=%1 [NS]
(The regexp ^/?(index\.php)?$ will match either an empty path or index.php, with or without a leading slash. That makes it a bit more complex than absolutely necessary, but also more robust. In particular, the /? lets it also work outside .htaccess files, where the leading slash will be present.)
Ps. The regexp ^char=(.*)$ will also allow URLs like /?char=foo&bar=baz to be rewritten to /game/CharWidget.swf?login=foo&bar=baz. If you don't want to allow such rewrites, replace it with e.g. ^char=([^&;]*)$.
Edit: Unfortunately, this isn't going to work for .swf files, because those execute on the client, and so won't see any changes to the query string made by server-side rewrites.
What you could do is make the rewrite external by replacing the [NS] flag with [NS,L,R=302]. However, this will also change the URL shown in the browser address bar, which may not be what you want. If so, another option would be to make the original request serve an HTML page on which you embed the .swf file.
I am terrible with mod_rewrite however I need to rewrite any request to the folder /files/users/*/ (* is a wildcard) to /view/ and insert the filename into a query paramater like so:
/files/users/9/test.pdf becomes /view/?file=test.pdf
How would I go about this assuming that the .htaccess file will be located inside /files/users/?
I would really appreciate if you explained how your solution works as I am slowly trying to become familiar with mod_rewrite.
So, you wanna have all my trade secrets on a silver plate?
Well, I try my best. ;-)
First of all, you must know where the documentation is. Look here for the reference: mod_rewrite. Or mod_rewrite, if your Apache version is 2.2.
You will find an overview with lots of links at Apache mod_rewrite. There, you will find a nice introduction to rewriting URLs. Also look here for lots of standard examples.
Since mod_rewrite supports PCRE regular expressions, you might need perlre and/or regular-expression.info from time to time.
Now to your question
RewriteEngine On
RewriteRule ^(?:.+?)/(.*) /view/?file=$1
This might already be sufficient. It looks for a subdirectory (?:.+?) in /files/users and captures the name of a file (.*) in this subdirectory. If this pattern matches, it rewrites the URL to /view/?file= and appends the captured file with $1, which gives /view/?file=$1.
All untested, of course, have fun.
P.S. Additional info is here at SO at .htaccess info and .htaccess faq.
Put the directive below in your .htaccess file to rewrite /files/users/9/test.pdf to /view/?file=test.pdf. In practical terms this means that if you visit http://yourdomain.com/files/users/9/test.pdf then the visitor will be served the rewritten url which is http://yourdomain.com/view?file=test.pdf
RewriteRule ^[^/]+/(.*)$ /view/?file=$1 [L]
A RewriteRule directive is part of the Apache mod_rewrite module. It takes two arguments:
Pattern - a regular expression to match against the current URL path (note that the URL path is not the entire URL but eg. /my/path, but in a .htaccess context the leading slash / is stripped giving us my/path).
Substitution - the destination URL or path where the user will rewritten OR redirected to.
Explaining the rule
The pattern ^[^/]+/(.*)$:
^ - the regex must match from the start of the string
[^/] - match everything but forward slash
+ - repetition operator which means: match 1 or more characters
/ - matches a forward slash
(.*) - mathes any characters. The dot means match any character. The star operator means match ANY characters (0 or more). The parantheses means the match is grouped and can be used in backreferences.
$ - the regex must match until the end of the string
The substitution /view/?file=$1:
...means that we rewrite the URL path to the /view/ folder with the query parameter file. The query parameter file will contain our first grouped match from the pattern as we pass it the $1 value (which means the first match from our RewriteRule pattern).
The [L] flag:
...means that mod_rewrite will stop processing rewrite rules. This is handy to avoid unwanted behaviour and/or infinite loops.
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.
Our current htaccess setup correctly converts urls like this: site.com/page.php?sid=Friend to site.com/Friend
However, due to an unrelated oversight, we had almost all of our URLs double-indexed as site.com/Friend> Because the greater than sign is a special character it doesn't call page.php so the > needs to be stripped out in htaccess and can't be done on page.php. Compounding matters is that the way they're indexed is as: site.com/Friend%3E which also might need to be stripped out.
What we would like is to have another directive that looks for an ending of > (or %3E), strips it off, then redirects to the variable that's there without that ending > In essence so that site.com/Friend> (or site.com/Friend%3E) still points to site.com/Friend
Thank you for your help.
Add this to the top of your rules:
RewriteRule ^/?(.*)>$ /$1 [L,R=301]
You can use > because the URI gets decoded when matching in a RewriteRule.
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