Forum Moderators: phranque
RewriteEngine On
# RewriteBase /
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?mydomain.com/.*$ [NC]
RewriteRule \.(gif|jpe?g|png)$ - [F]
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://www.mydomain.com/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/(.*)$ test/rewrite.php?url=$1 [L]
RewriteRule ^(.*)$ rewrite.php?url=$1 [L] "www" prefix appended http://www.mydomain.com/test/myfile.php I would like to redirect to the path test/rewrite.php test/ prefix are then redirected to root file rewrite.php rewrite.php. In the final condition, there are 2 rules which I would like to perform:
!^www.example.com$ pattern as well as stopping this rule causing an infinite redirect loop for HTTP/1.0 requests. Use: !^(www\.example\.com)?$. Every time mod_rewrite encounters an [L], it stops what it's doing.It only stops if all the patterns for this rule actually matched.
RewriteEngine On
# RewriteBase /
##### Protect images from hotlinking
RewriteCond %{HTTP_REFERER} !^-?$
RewriteCond %{HTTP_REFERER} !^http://(www.)?example.com/.*$ [NC]
RewriteRule \.(gif|jpe?g|png)$ - [F]
##### Resolve index.html to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://www.example.com/ [R=301,L]
##### Resolve canonical domains
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]
##### Rewrite function
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/(.*)$ test/rewrite.php?url=$1 [L]
RewriteRule ^(.*)$ rewrite.php?url=$1 [L] http://www.example.com/test/file.php the rewrite maps to the rewrite.php?url=$1 file. Although from my understanding I would have thought that the 1st rewrite rule would in fact rewrite to test/rewrite.php?url=$1. Is there 2 rewrites going on here?
.*$ part of the pattern is redundant and can simply be omitted. You should also escape all literal periods in that pattern (2 to do). index\.html to index\.html? (2 to do) so that requests for .htm as well as for .html are both redirected. RewriteEngine On
# RewriteBase /
##### Protect images from hotlinking
RewriteCond %{HTTP_REFERER} !^-?$
RewriteCond %{HTTP_REFERER} !^http://(www.)?example.com/.*$ [NC]
RewriteRule \.(gif|jpe?g|png)$ - [F]
##### Resolve index.html to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html?\ HTTP/
RewriteRule ^index\.html$ http://www.example.com/ [R=301,L]
##### Resolve canonical domains
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
##### Rewrite function 1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/(.*)$ test/rewrite.php?url=$1 [L]
##### Rewrite function 2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ rewrite.php?url=$1 [L] RewriteConds apply only to the very next RewriteRule that follows.
########## Begin - Joomla! core SEF Section
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
#
########## End - Joomla! core SEF Section RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !^/index.php RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC] RewriteRule (.*) index.php RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L] RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/(.*)$ test/rewrite.php?url=$1 [L]
RewriteRule ^(.*)$ rewrite.php?url=$1 [L] http://www.example.com/test/myfile.php, why do I observe the final page served as being the 2nd rule page (rewrite.php) that processes the request?
RewriteEngine On
RewriteBase /
##### Protect images from hotlinking
RewriteCond %{HTTP_REFERER} !^-?$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com\.au/ [NC]
RewriteRule \.(gif|jpe?g|png)$ - [F]
##### Resolve canonical domains
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
##### Resolve index.html to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html?\ HTTP/
RewriteRule ^index\.html?$ http://www.example.com.au/ [R=301,L]
##### Resolve index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.example.com.au/ [R=301,L]
##### Rewrite function 1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/(.*)$ blog-article.php?url=$1 [L]
##### Rewrite function 2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ article.php?url=$1 [L]
##### Resolve index.php, index.html and index.htm to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php|index\.html?)\ HTTP/
RewriteRule ^index\.php$ http://www.example.com.au/ [R=301,L]
##### Resolve contact.html to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /contact\.html?\ HTTP/
RewriteRule ^contact\.html?$ http://www.example.com.au/contact-us.html [R=301,L]
##### Resolve about.html to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /about\.html?\ HTTP/
RewriteRule ^about\.html?$ http://www.example.com.au/about-us.html [R=301,L]
"our services.html"
1. Can I simplify the index.html and index.php redirect rules into one rule?
##### Resolve index.php, index.html and index.htm to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php|index\.html?)\ HTTP/
RewriteRule ^index\.php$ http://www.example.com.au/ [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.(php|html?)\ HTTP/ 2. I also have specific pages which will need to be 301 redirected to a new page. These were originally static html pages. e.g. contact.html, about.html, faq.html etc.... Should I create a separate rule in my htaccess file just for these unique pages (5 in total)?
RewriteRule ^(contact|about)\.html?$ http://www.example.com.au/$1-us.html [R=301,L]
3. Finally if my web page name has a space character in it, how would match this in my expression?
e.g. "our services.html"
our\ services\.html
RewriteEngine On
RewriteBase /
##### Protect images from hotlinking
RewriteCond %{HTTP_REFERER} !^-?$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com/ [NC]
RewriteRule \.(gif|jpe?g|png)$ - [F]
##### Resolve canonical domains
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
##### Resolve index html/php page to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.(php|html?)\ HTTP/
##### Resolve static html pages to static version
RewriteRule ^(about|company|services|support|contact)\.html?$ http://www.example.com/$1-static.html [R=301,L]
##### Rewrite function 1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/(.*)$ blog-article.php?url=$1 [L]
##### Rewrite function 2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ article.php?url=$1 [L]