https://www.digitalocean.com/community/tutorials/how-to-set-up-mod_rewrite-for-apache-on-ubuntu-14-04
1 ubuntu@ubuntu:~$ su 2 Password: 3 root@ubuntu:/home/ubuntu# a2enmod rewrite 4 Enabling module rewrite. 5 To activate the new configuration, you need to run: 6 service apache2 restart 7 root@ubuntu:/home/ubuntu# service apache2 restart
https://httpd.apache.org/docs/2.4/en/rewrite/
将任意的url 映射为服务器内部的url结构
mod_rewrite
provides a way to modify incoming URL requests, dynamically, based on regular expression rules. This allows you to map arbitrary URLs onto your internal URL structure in any way you like.
It supports an unlimited number of rules and an unlimited number of attached rule conditions for each rule to provide a really flexible and powerful URL manipulation mechanism. The URL manipulations can depend on various tests: server variables, environment variables, HTTP headers, time stamps, external database lookups, and various other external programs or handlers, can be used to achieve granular URL matching.
Rewrite rules can operate on the full URLs, including the path-info and query string portions, and may be used in per-server context (httpd.conf
), per-virtualhost context (<VirtualHost>
blocks), or per-directory context (.htaccess
files and <Directory>
blocks). The rewritten result can lead to further rules, internal sub-processing, external request redirection, or proxy passthrough, depending on what flags you attach to the rules.
Since mod_rewrite is so powerful, it can indeed be rather complex. This document supplements the reference documentation, and attempts to allay some of that complexity, and provide highly annotated examples of common scenarios that you may handle with mod_rewrite. But we also attempt to show you when you should not use mod_rewrite, and use other standard Apache features instead, thus avoiding this unnecessary complexity.
- mod_rewrite reference documentation
- Introduction to regular expressions and mod_rewrite
- Using mod_rewrite for redirection and remapping of URLs
- Using mod_rewrite to control access
- Dynamic virtual hosts with mod_rewrite
- Dynamic proxying with mod_rewrite
- Using RewriteMap
- Advanced techniques
- When NOT to use mod_rewrite
- RewriteRule Flags
- Technical details
Character | Meaning | Example |
---|---|---|
. |
Matches any single character |
c.t will match cat , cot , cut , etc. |
+ |
Repeats the previous match one or more times |
a+ matches a , aa , aaa , etc |
* |
Repeats the previous match zero or more times. |
a* matches all the same things a+ matches, but will also match an empty string. |
? |
Makes the match optional. |
colou?r will match color and colour . |
^ |
Called an anchor, matches the beginning of the string |
^a matches a string that begins with a
|
$ |
The other anchor, this matches the end of the string. |
a$ matches a string that ends with a . |
( ) |
Groups several characters into a single unit, and captures a match for use in a backreference. |
(ab)+ matches ababab - that is, the + applies to the group. For more on backreferences see below. |
[ ] |
A character class - matches one of the characters |
c[uoa]t matches cut , cot or cat . |
[^ ] |
Negative character class - matches any character not specified |
c[^/]t matches cat or c=t but not c/t
|