mod_rewrite cheat sheet

mod_rewrite Tutorials

http:/­/ht­tpd.ap­ach­e.o­rg/­doc­s/c­urr­ent­/re­write/

http:/­/ww­w.a­dde­dby­tes.co­m/f­or-­beg­inn­ers­/ur­l-r­ewr­iti­ng-­for­-be­gin­ners/

http:/­/ne­t.t­uts­plu­s.c­om/­tut­ori­als­/ot­her­/a-­dee­per­-lo­ok-­at-­mod­_re­wri­te-­for­-ap­ache/

mod_rewrite RewriteRule Flags

C

Chained with next rule

CO=cookie

Set specified cookie

E=var:­value

Set enviro­nmental variable “var” to “value”

F

Forbidden (403 header)

G

Gone – no longer exists

H=handler

Set handler

L

Last – stop processing rules

N

Next – continue processing

NC

Case insens­itive

NE

Do not escape output

NS

Ignore if subrequest

P

Proxy

PT

Pass through

R[=code]

Redirect to new URL, with optional code (see below)

QSA

Append query string

S=x

Skip next x rules

T=mime­-type

Set mime type

mod_rewrite RewriteCond Flags

NC

Case insens­itive

OR

Combine with next rule using ‘OR’ instead of the default of ‘AND’

Redirection Header Codes

301

Moved perman­ently

302

Moved tempor­arily (default)

mod_rewrite Directives

Rewrit­eEngine

Rewrit­eOp­tions

RewriteLog

Rewrit­eLo­gLevel

Rewrit­eLock

RewriteMap

Rewrit­eBase

Rewrit­eCond

Rewrit­eRule

Regular Expressions Syntax

^

Start of string

$

End of string

.

Any single character

(a|b)

a or b

(…)

Group section

[abc]

In range (a, b or c)

[^abc]

Not in range

\s

White space

a?

Zero or one of a

a*

Zero or more of a

a*?

Zero or more, ungreedy

a+

One or more of a

a+?

One or more, ungreedy

a{3}

Exactly 3 of a

a{3,}

3 or more of a

a{,6}

Up to 6 of a

a{3,6}

3 to 6 of a

a{3,6}?

3 to 6 of a, ungreedy

\

Escape character

[:punct:]

Any punctu­ation symbol

[:space:]

Any space character

[:blank:]

Space or tab

There’s an excellent regular expression tester at: http:/­/re­gex­pal.com/

mod_rewrite Server Variables: HTTP Headers

%{HTTP­_US­ER_­AGENT}

%{HTTP­_RE­FERER}

%{HTTP­_CO­OKIE}

%{HTTP­_FO­RWA­RDED}

%{HTTP­_HOST}

%{HTTP­_PR­OXY­_CO­NNE­CTION}

%{HTTP­_AC­CEPT}

mod_rewrite Server Variables: Server Internals

%{DOCU­MEN­T_ROOT}

%{SERV­ER_­ADMIN}

%{SERV­ER_­NAME}

%{SERV­ER_­ADDR}

%{SERV­ER_­PORT}

%{SERV­ER_­PRO­TOCOL}

%{SERV­ER_­SOF­TWARE}

mod_rewrite Sample Rule: Site Moved

# Site moved perman­ently

Rewrit­eCond %{HTTP­_HOST} ^www.d­oma­in.com$ [NC]

Rewrit­eRule ^(.*)$ http:/­/ww­w.d­oma­in2.com/$1 [R=301,L]

Rewrites domain.com to domain­2.com

mod_rewrite Sample Rule: Temporary Page Move

# Page has moved tempor­arily

Rewrit­eRule ^page.h­tml$ new_pa­ge.html [R,NC,L]

Rewrites domain.co­m/p­age.html to domain.co­m/n­ew_­pag­e.html

mod_rewrite Sample Rule: Nice URLs

# Nice URLs (no query string)

Rewrit­eRule ^([A-Z­a-z­0-9­-]+)/?$ catego­rie­s.p­hp?­name=$1 [L]

Rewrites domain.co­m/c­ate­gor­y-n­ame-1/ to domain.co­m/c­ate­gor­ies.ph­p?n­ame­=ca­teg­ory­-name-1

mod_rewrite Server Variables: Special

%{API_­VER­SION}

%{THE_­REQ­UEST}

%{REQU­EST­_URI}

%{REQU­EST­_FI­LENAME}

%{IS_S­UBREQ}

%{HTTPS}

mod_rewrite Server Variables: Request

%{REMO­TE_­ADDR}

%{REMO­TE_­HOST}

%{REMO­TE_­PORT}

%{REMO­TE_­USER}

%{REMO­TE_­IDENT}

%{REQU­EST­_ME­THOD}

%{SCRI­PT_­FIL­ENAME}

%{PATH­_INFO}

%{QUER­Y_S­TRING}

%{AUTH­_TYPE}

mod_rewrite Server Variables: Time

%{TIME­_YEAR}

%{TIME­_MON}

%{TIME­_DAY}

%{TIME­_HOUR}

%{TIME­_MIN}

%{TIME­_SEC}

%{TIME­_WDAY}

%{TIME}

Comments
26 Responses to “mod_rewrite cheat sheet”
  1. Tonja Lykens says:

    I just want to say I am newbie to blogging and definitely liked you’re web blog. Almost certainly I’m planning to bookmark your website . You amazingly have really good stories. Many thanks for revealing your webpage.

  2. Very interesting topic, thank you for posting.

  3. Taruhan Bola says:

    Very interesting topic, appreciate it for posting.

  4. Hey. Self-same cool website!! Guy .. Magnificent .. Amazing .. I will bookmark your website and obtain the feeds additionally…I’m happy to find numerous helpful in rank right now within the article. Be grateful you for sharing…

  5. Very interesting subject, thanks for putting up.

  6. Kasha Marini says:

    Excellent issue: will be the design template on your site has been done to help purchase or can be purchased general? Since I must say i appreciated and wish to utilize similar to that will working for you. Thank you so much before hand on your response.

  7. I really enjoy looking through on this web site , it holds superb content .

  8. Mana Yanda says:

    Thank you for sharing excellent informations. Your website is very cool. I am impressed by the details that you have on this site. It reveals how nicely you perceive this subject. Bookmarked this website page, will come back for more articles. You, my friend, ROCK! I found just the information I already searched everywhere and just couldn’t come across. What a perfect site.

  9. Useful info. Lucky me I found your website by accident, and I am shocked why this coincidence did not happened in advance! I bookmarked it.

  10. I consider this is among the so very much significant info personally. And now i’m glad studying your article. However want to observation upon some common issues, The site taste is excellent, the articles is actually nice: D. Good job, cheers

  11. Hi my loved one! I wish to say that this post is amazing, great written and include approximately all significant infos. I’d like to look extra posts like this .

  12. Aide Truett says:

    I intended to write you this bit of note in order to thank you so much yet again for the magnificent concepts you’ve shown on this site. This has been certainly extremely open-handed with people like you to provide without restraint all that a number of people would have advertised for an e book to get some cash for themselves, and in particular considering that you could possibly have tried it if you ever decided. Those advice in addition served as the fantastic way to fully grasp the rest have the identical zeal just as my own to realize a little more around this matter. I am certain there are a lot more enjoyable opportunities in the future for many who browse through your blog.

  13. next says:

    A person essentially assist to make severely articles I might state. This is the first time I frequented your web page and up to now? I amazed with the analysis you made to make this actual put up extraordinary. Wonderful job!

  14. I am impressed with this site, real I am a fan.

  15. This blog was… how do you say it? Relevant!! Finally I’ve found something that helped me. Cheers!

  16. Im grateful for the post.Really looking forward to read more. Really Cool.

  17. I enjoy your current method to the problem, really healthful along with individual. Very well you may still find folks who may spend their selves about what they certainly, in addition to most likely therefore , as a result leading his edge. Best of luck!

  18. purificadoras says:

    I’m new to your blog and i really appreciate the nice posts and great layout.:.;,’

  19. I blog frequently and I really thank you for your content. Your article has truly peaked my interest. I’m going to take a note of your site and keep checking for new details about once a week. I subscribed to your RSS feed too.

  20. Louis Max says:

    Good post! We are linking to this great content on our website. Keep up the good writing.

  21. Excellent web site you’ve got here.. It’s difficult to find high-quality writing like yours nowadays. I honestly appreciate individuals like you! Take care!!

  22. Jed Mcclenty says:

    Amaze, astounding site system! Just how long were you blogging and site-building for the purpose of? You have made operating a blog appearance quick. The complete start looking of the online site can be great, much less this content! Great report!

  23. There is definately a great deal to find out about this subject. I really like all the points you’ve made.

  24. bingo hein says:

    Fantastic site. Plenty of helpful info here. I am sending it to a few buddies ans also sharing in delicious. And certainly, thank you for your sweat!

  25. I like reading through an article that will make people think. Also, thank you for allowing for me to comment!

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.