RewriteRule example
piaoling 2011-04-25 11:18:46
-
If
http://example.com/foo/bar
does not exist, redirect tohttp://other.example.com/foo/bar
. (Put this in an .htaccess file in your top-level web directory.)# .htaccess in root of example.com RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ http://other.example.com/$1 [R]
-
Handle all requests for top-level
.html
files and files with no extensions (http://example.com/foo
,http://example.com/foo.html
) with a single PHP program/foo/show.php
. Also, ignore trailing characters in set {: ; , .
} so URLs like "http://example.com/foo.
" can be copied-n-pasted from plain text sentences by inattentive users.# .htaccess in root of example.com RewriteRule ^/?([^/]*.html?|[^./]*)[:;,.]*$ /foo/show.php [L,NS]
Examples:http://tomclegg.net/rewriterule http://tomclegg.net/rewriterule.html;
-
Redirect GET requests for
http://example.com/foo/bar
tohttp://example.com/bar
(and/foo/bar.html
to/bar.html
). Handle POST requests with PHP program rather than attempting to redirect a POST (which is unlikely to work well).# .htaccess in foo folder in example.com's document root RewriteEngine On RewriteCond %{REQUEST_METHOD} GET RewriteRule ^/?([^/]*.html?|[^./]*)[:;,.]*$ /$1 [R,L,NS] RewriteCond %{REQUEST_METHOD} POST RewriteRule ^/?([^/]*.html?|[^./]*)[:;,.]*$ /foo/show.php [L,NS]
Examples:http://tomclegg.net/w/rewriterule http://tomclegg.net/w/rewriterule.html; My own Examples: RewriteEngine On #RewriteCond %{REQUEST_METHOD} GET RewriteRule ^bloglist/([.]*)$ bloglist.php [NC] RewriteRule ^bloglist/([0-9]+)$ bloglist.php?blogId=$1 [NC] RewriteRule ^bloglist/bloglist.php(.*)$ http://www.localhost.com/connection/bloglist.php$1 [NC,NE,R] RewriteRule ^bloglist.php/([0-9]+)$ http://www.localhost.com/connection/bloglist.php?userId=$1 [NC] RewriteRule ^bloglist.php/bloglist.php(.*)$ http://www.localhost.com/connection/bloglist.php$1 [NC,R] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ http://www.localhost.com/connection/404.php [R]
发表评论(评论将通过邮件发给作者):