ErrorDocument 404 /errors/pageNolongerExists.html
An ErrorDocument directive can be added to either Apache’s main httpd.conf (sometimes called: apache.conf) or via a .htaccess file within the sites document tree. It’s recommended that at least one stylized page be provided per site, ideally with some helpful text and a few working links, so as to not overly worry customers with a sticky keyboard or dyslexic fingers. While not presenting the user with a white Screen the ErrorDocument approach does not provide a seamless transition for the User, so Redirection or Rewriting is a popular choice. Before I go further I should explain the difference between Redirection and Rewriting.
<meta http-equiv="refresh" content="0;url=http://www.newsite.com/newLocation/">
The above tag will immediately ask the User’s browser to redirect to page: http://www.newsite.com/newLocation/
# Specify the new, absolute, location for the content that used to be found in /oldDir
Alias /oldDir /srv/www/htdocs/newDirectory
# Specify the new location for any jpeg files that were formerly in the products directory
AliasMatch ^/products/(.*\.jpg)$ /srv/www/htdocs/images/$1
# Note: For an explanation of (.*\.jpg)$ and $1 see: Regular Expressions
# Indicates the content of /oldDirectory can now be found on a different Web-site.
Redirect /oldDirectory http://www.newsite.com/somDirectory
# A Temporary redirection within the site, should not alter the Visible URL in the address bar.
RedirectTemp /oldDirectory http://www.oldSite.com/newDirectory
# Off load all requests for images to a remote Content Management service.
RedirectMatch ([^.\/]*\.jpg)$ http://www.contentManagementService.com/someDirectory/$1
It should be noted that Alias directives can only be added to the httpd.conf, whilst Redirect* directives can also be added to a .htaccess file, assuming permissions to been granted via a suitable: Options FollowSymLinks directive.
# URI pattern only
RewriteRule <URI Pattern> <New URL> [Response codes and modifiers]
# URI pattern AND a Conditional Rule:
RewriteCond %{<Some variable>} <Pattern> [Logical operation and modifiers]
RewriteRule <URI Pattern> <New URL> [Response code, Operation or modifier]
Note: a rule can have multiple Conditions, joined via logical AND's (default) or OR's e.g.
#Required just once - enables Rewriting.
RewriteEngine On
#Internaly Alter (Alias) the first 2 characters of URI PATH's starting: /VV to: /XX, ignoring the case, and continue on to the next rule (default action).
RewriteRule ^/?VV(.*) /XX$1 [NC]
# If the requested PATH start with “XX", followed by one ore more non / characters, and ends in a /
# AND that does not resolve to File, Directory or Symbolic link
# Then: internaly redirect the request to the /index.php page and pass the rest of the URI, the bit after /XX in the CGI variable: restOfURI,
# and ignore all further RewriteRule's
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^/?XX([^/]+)/$ /index.php?restOfURI=$1 [NC,L]
# If the request is for page /products/show.php
# And a (passed category parameter of value {20,21,22,23,24,25}
# Or product code starting with 'a' ot 'A' and followed by a series of digits)
# Then internaly redirect the request to page: /products/temptNotAvailable.html, and ignore all other RewriteRule
RewriteCond %{QUERY_STRING} catogory=2[0-5] [NC,OR]
RewriteCond %{QUERY_STRING} product=a[0-9]+ [NC]
RewriteRule ^/?products/show.php /products/temptNotAvailable.html [L]
# .htaccess Format
RewriteRule ^XX([^/]+) index.php [NC,L]
# httpd.conf Format
RewriteRule ^/XX([^/]+) index.php [NC,L]
# Will work in either, but less efficient
RewriteRule ^/?XX([^/]+) index.php [NC,L]
RewriteLog /tmp/tmp_rewrite.log
RewriteLogLevel 9
Once in place just browse to the erroneous url, then check the log file. The log will indicate the values it's comparing with each patterns in your Rules. Once you have identified and resolved the issue remember to either remove the lines or set the RewriteLogLevel to 0, and restart Apache.
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
#LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
#LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
#LoadModule jk_module modules/mod_jk.so
#LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
ProxyRequests Off
ProxyPass /application http://www.otherSite.com/bar
ProxyPassReverse /application http://www.otherSite.com/bar
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
#The following directive, can be made conditional:
ProxyPass /application http://www.otherSite.com/bar
# e.g. Only Proxy requests for HTTP user: Fred
RewriteCond %{REMOTE_USER} fred [NC]
RewriteRule /application http://www.otherSite.com/bar [P,L]
RewriteRule /application - [F]
Note: You still require the rest of the Proxy directive above, when using a RewriteRule instead of a ProxyPass.
RewriteCond %{REQUEST_URI} ^/aaa/ [OR]
RewriteCond %{REQUEST_URI} ^/bbb/ [OR]
RewriteCond %{REQUEST_URI} ^/ccc/
...
Could be re-written using the regular expression:
RewriteCond %{REQUEST_URI} ^/(aaa|bbb|ccc)/
...
Similarly the following parameter matching logic:
RewriteCond %{QUERY_STRING} cat=1 [OR]
RewriteCond %{QUERY_STRING} cat=2 [OR]
RewriteCond %{QUERY_STRING} cat=3
...
Could be re-written using the regular expression:
RewriteCond %{QUERY_STRING} cat=[1-3]
...
As well as simplifying the process you’ll probably find a basic grasp of Regular Expressions will be vital in achieving what you want. So I suggest you download and read the following cheat sheets, along with the covering articles, then possibly scan over my scribblings below:
! Not the following pattern
( ) Assign the contents of the pattern inside the braces, to a variable.
[ ] Any of the characters enclosed in these braces
(a|b) Either a or B
. Any character
\. A Period character
\\ A Back slash character
\( A brace
? ZERO or more occurrences of the previous pattern
* ZERO or more occurrences of the previous pattern
+ One or more occurrences of the previous pattern
A few common regular expressions in use:
[A-Z0-9] An Alphanumeric character
[^/\-_] Not any of the following characters: / – _
^/?xxx An optional leading /, so either /xxx or xxx will match
xxx/?$ An optional trailing /, so either xxx/ or xxx will match
[A-Z]* ZERO or more Alphabetic characters
[A-Z]+ One or more Alphabetic characters
! /xxxx Not String /xxxx
/(cat|dog)/ Either /cat/ or /dog/
Note:
#Convert URI: /JumpedTheQuickFoxBrown.html to: /The_Quick_Brown_Fox_Jumped.html
RewriteCond %{REQUEST_URI} /?Jumped(The)Quick(Fox)Brown\.html
RewriteRule /?(Jumped)The(Quick)Fox(Brown)\.html /%1_$2_$3_%2_$1.html [L,R=301]
If you wish to test the above rule simple add a file by the name of: The_Quick_Brown_Fox_Jumped<HTML>This is a test<HTML>
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
2) If the requested PATH start with “XX", and does not resolve to a File, Directory or Symbolic link redirect to the index.php page:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^XX([^/]+) index.php [NC,L]
3) How do you apply a condition to several rules, simple by skipping 'n' rules if the condition does not match e.g.
#Skip the next 3 rules IF Not a request for /product/special
RewriteCond %{REQUEST_URI} ! /product/special [NC]
RewriteRule .* - [S=3]
RewriteRule /?product/special/([^/])+$ /product/main.php?cat=special&month=$1 [L]
RewriteRule /?product/special/([^/])+/([^/])+$ /product/main.php?cat=special&month=$1&cat=$2 [L]
RewriteRule /?product/special/([^/])+/([^/])+/([^/])+$ /product/main.php?cat=special&month=$1&cat=$2&item=$3 [L]
# End of /product/special block
4) Block access to your image, unless the request has apparently come from a page on yourSite:
# Only accept image request from your own site
RewriteCond %{HTTP_REFERER} !www\.yoursite\.com [NC]
RewriteRule \.(jpg|gif|png) - [F,L]
5) How to make the contents of a sub-directory appear to be your Document root, with a few exceptions for admin and status app's:
# Rewrite all request for URL's not starting with: /theSubDir, /status, /admin or /manager into the /theSubDir/
RewriteCond %{REQUEST_URI} !/(theSubDir|status|admin|manager)
RewriteRUle .* /theSubDir%{REQUEST_URI} [L]
6) Only permit indirect access to your index.php, via an internal redirect from another URI:
#Attempt to block direct requests to index.php script, only internally rewritten requests should get through
RewriteRule ^/?AppName index.php [L,NC]
RewriteCond %{THE_REQUEST} index\.php [NC]
RewriteRule index.php - [F,NC]
7) Force all requests for files in the /secure/ directory to HTTPS (Assumes your server already has a set of SSL certificates installed) and for /insecure/ files to http:
RewriteCond %{HTTPS} !on
RewriteRule ^/?secure/ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteRule ^/?insecure/ http://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (4)
Commented:
Also an example to find anything but few words would be worth.
e.g * but (xyz|images|css|other)
Author
Commented:The following condition will ensure the QUERY_STRING is blank:
Open in new window
Commented:
Can you look into this query and help us?
https://www.experts-exchange.com/questions/27089193/Expert-help-with-RewriteRules.html
Commented: