Was just wondering if someone would be kind enough to look at my htaccess code and tell me if there is anything that looks obviously incorrect. I don't know much about this stuff. Also not sure if the parts should be organize differently or if it is necessary to put
"RewriteEngine on
RewriteBase /"
in there more than once?
Anyways, here is the code:
RewriteEngine on
RewriteBase /
RewriteRule ^(.+)\.shtml$ $1.html [R=301,L]
redirect 301 /games.html http://www.slimekids.com/games/
redirect 301 /trailers.html http://www.slimekids.com/book-trailers/
redirect 301 /authors.html http://www.slimekids.com/authors/
redirect 301 /reference.html http://www.slimekids.com/reference/
redirect 301 /reviews.html http://www.slimekids.com/book-reviews/
redirect 301 /searches.html http://www.slimekids.com/search-engines/
RewriteEngine on
RewriteBase /
#if the domain is not www.slimekids.com
RewriteCond %{HTTP_HOST} !^www\.slimekids\.com$ [NC]
#redirect to www.slimekids.com
RewriteRule ^(.*)$ http://www.slimekids.com/$1 [L,R=301]
#leave this rule in place, but after the one above to handle the home page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://www.slimekids.com/ [R=301,L]
ErrorDocument 404 /404page.html
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES CACHING ##
Code looks fine.
You are correct that the RewriteEngine on, and RewriteBase / don't need to be repeated.
I notice the the exprires are quite far into the future. e.g. image expire after one year. Might be that your code uses cachebusting of some kind when something changes.
I would probably slightly alter the lines that remove the index.html part from the url, to also work on subfolders (e.g. /games/index.html), instead of just the root (/index.html)
#leave this rule in place, but after the one above to handle the home page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ http://www.slimekids.com/$1 [R=301,L]
New and improved htaccess. Hopefully this all looks good now. Thanks very much.
RewriteEngine on
RewriteBase /
RewriteRule ^(.+)\.shtml$ $1.html [R=301,L]
redirect 301 /games.html http://www.slimekids.com/games/
redirect 301 /trailers.html http://www.slimekids.com/book-trailers/
redirect 301 /authors.html http://www.slimekids.com/authors/
redirect 301 /reference.html http://www.slimekids.com/reference/
redirect 301 /reviews.html http://www.slimekids.com/book-reviews/
redirect 301 /searches.html http://www.slimekids.com/search-engines/
#if the domain is not www.slimekids.com
RewriteCond %{HTTP_HOST} !^www\.slimekids\.com$ [NC]
#redirect to www.slimekids.com
RewriteRule ^(.*)$ http://www.slimekids.com/$1 [L,R=301]
#leave this rule in place, but after the one above to handle the home page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ http://www.slimekids.com/$1 [R=301,L]
ErrorDocument 404 /404page.html
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 month"
ExpiresByType image/jpeg "access 1 month"
ExpiresByType image/gif "access 1 month"
ExpiresByType image/png "access 1 month"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 month"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES CACHING ##
Related
So when I was testing my website which is hosted on Bluehost, I wanted to test out my 404.html which was going well. However, I then noticed that I keep getting the default 500 internal server error from Bluehost when I typed something like this, https://www.example.com/folder/webpage/awdawdasd (basically putting a slash after the webpage name and typing random things). In my htaccess, I made it so that it allows users to visit the website without the .html at the back, i.e. hides the extension in the URL. How can I fix it so that when the user type random things after the html page like shown in the example?
Note that I am using Bluehost's file manager only, i.e. hosting my own html pages on Bluehost that I have coded as part of my assignment, I am not using Wordpress or anything.
Here is my htaccess code,
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php74” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
# Prevent user from going to the website's index
Options -Indexes
</IfModule>
## EXPIRES HEADER CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
# Images
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType image/x-icon "access plus 1 year"
# Video
ExpiresByType video/webm "access plus 1 year"
ExpiresByType video/mp4 "access plus 1 year"
ExpiresByType video/mpeg "access plus 1 year"
# Fonts
ExpiresByType font/ttf "access plus 1 year"
ExpiresByType font/otf "access plus 1 year"
ExpiresByType font/woff "access plus 1 year"
ExpiresByType font/woff2 "access plus 1 year"
ExpiresByType application/font-woff "access plus 1 year"
# CSS, JavaScript
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
# Others
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType image/vnd.microsoft.icon "access plus 1 year"
</IfModule>
## REWRITE RULES
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirects user from http to https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
# Allow browsers to access webpages without .html at the end
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
# Redirect user to webpage url without .html if they typed .html in the url
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
# Removes .php extension
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
# Redirect user to webpage url without .php if they typed .php in the url
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
# Redirect users to the error webpages if user receives a 404 error, etc.
ErrorDocument 401 /errorpages/401
ErrorDocument 403 /errorpages/403
ErrorDocument 404 /errorpages/404
ErrorDocument 500 /errorpages/500
ErrorDocument 502 /errorpages/502
ErrorDocument 503 /errorpages/503
# End of Apache Rewrite Rules
</IfModule>
# php -- END cPanel-generated handler, do not edit
Bluehost 500 Internal Server Error Message Image
Edit: I have contacted Bluehost and was able to deduce that it was not from their side but the problem lies in the code above. I have also edited the apache rewrite code to redirect users if they type in .html at the back.
Update: Managed to find the error logs and it says
/home2/otakuabr/public_html/.htaccess: RewriteRule: bad flag delimiters
Just a simple question. In a -htaccess file, I can set a [L] flag. Does this "L" only ignore if rewrite stuff come after or the complete code:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On
# Default directive
ExpiresDefault "access plus 1 month"
# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
# CSS
ExpiresByType text/css "access plus 1 month"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
</IfModule>
In other words, are the lines after the L-flag considered or is it better to put in always on last line?
The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed.
http://httpd.apache.org/docs/current/en/rewrite/flags.html#flag_l
So, in your case, the following instructions are not affected.
I want to 301 redirect a couple old URLs
http://www.mysite.com/contact/old-page.php
http://www.mysite.com/contact/another-old-page.php
to this one
http://www.mysite.com/contact/
I'm pretty sure my code is correct, since I have used this on other sites with no problem. I know redirecting is enabled because the non-www to www redirect works just fine. But for some reason this one-off redirect doesn't work; nothing happens at all when I visit the old page, as if I didn't do anything.
Here's the complete .htaccess file:
#Prevent viewing of htaccess
<Files .htaccess>
order allow,deny
deny from all
</Files>
#Enable symbolic links
Options +FollowSymLinks
RewriteEngine On
#Force www version of URL
RewriteCond %{HTTP_HOST} ^mysite\.com [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
#This is the bit that isn't working
Redirect 301 /contact/old-page.php http://www.mysite.com/contact/
Redirect 301 /contact/another-old-page.php http://www.mysite.com/contact/
#Gzip
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/php text/plain text/xml text/css text/javascript application/javascript application/x-javascript
</ifmodule>
#Caching
<ifmodule mod_expires.c>
ExpiresActive On
ExpiresByType image/gif "access 1 month"
ExpiresByType image/png "access 1 month"
ExpiresByType text/css "access 1 week"
ExpiresByType text/javascript "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresByType application/x-javascript "access 1 month"
ExpiresByType image/x-icon "access 1 month"
</ifmodule>
Your redirects look fine, but one possible conflict is that you're using mod_alias (Redirect) and mod_rewrite (RewriteRule) together and the URI passes through both modules and both modules can process them, not independently of each other. And sometimes this causes unexpected results.
Try just using rewrite rules and remove the Redirect statements. Try adding these two redirect rules before your "force www" rule:
RewriteRule ^contact/old-page\.php$ http://www.mysite.com/contact/ [L,R=301]
RewriteRule ^contact/another-old-page\.php$ http://www.mysite.com/contact/ [L,R=301]
I have a .htaccess question. Basically my .htaccess is redirecting my newly created sub domain, to the main site, and I don't want it to do so. Let's assume my domain is called 'www.beans.com', and the subdomain is 'shop.beans.com', which is in the public_html folder under /shop/. Here is the .htaccess:
DirectoryIndex index.php enmain.php
ErrorDocument 404 /404.html
## EXPIRES CACHING ##
ExpiresActive On
ExpiresByType image/jpg "access plus 1 week"
ExpiresByType image/jpeg "access plus 1 week"
ExpiresByType image/gif "access plus 1 week"
ExpiresByType image/png "access plus 1 week"
ExpiresByType image/ico "access plus 1 year"
ExpiresByType text/css "access plus 1 week"
ExpiresByType text/javascript "access plus 1 week"
ExpiresByType text/x-javascript "access plus 1 week"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresByType text/xml "access plus 2 hours"
ExpiresDefault "access plus 1 hour"
## EXPIRES CACHING ##
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.beans.com$ [NC]
RewriteRule ^(.*)$ http://www.beans.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.(html?|php)(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]*/)*)index\.(html?|php)$ http://www.beans.com/$1 [R=301,L]
# Start CloudFlare:beans.com rewrite. Do not Edit
RewriteEngine On
RewriteCond %{HTTP_HOST} ^beans.com
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# End CloudFlare rewrite.
So in summary, I would like when I browse to 'shop.beans.com' not to be redirected to 'www.beans.com' (which currently happening).
How can I do it?
First thing you have 2 rules doing same thing i.e. adding www..
1 - Delete this rule:
RewriteCond %{HTTP_HOST} !^www.beans.com$ [NC]
RewriteRule ^(.*)$ http://www.beans.com/$1 [L,R=301]
2 - Then change last rule like this:
RewriteCond %{HTTP_HOST} ^beans\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
There must be a rule in your .htaccess file in which you set redirect all urls that are opening witout www to with www. you don't need to change this file.
you should do this:
create a .htaccess file in your subdomain and add the following code in it.
RewriteEngine on
RewriteBase /
save this file.
Now all the things work properly.
We have a WP Multisite install with a main site and one sub site.
We just purchased UCC Certificate from Godaddy and wish to secure the entire site with https.
We are looking to:
redirect domain.com to www.domain.com
redirect http://www.domain.com to https://www.domain.com
In other words, force www and https on everything..
I have tried a few modifications to our .htaccess file without success. Seems to cause problems with Google Chrome.
Thanks in advance for any feedback or advice!
S Cranston
Here is my current .htaccess file:
## EXPIRES CACHING ##
<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On
# Default directive
ExpiresDefault "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
# CSS
ExpiresByType text/css "access 1 month"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
<IfModule mod_headers.c>
SetEnvIf Origin "http(s)?://(www\.)?(www.simplymusicteachers.com/wp-admin/admin-ajax.php|www.simplymusicteachers.com)$" AccessControlAllowOrigin=$0$1
Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
</IfModule>
And here is the file with the additional code added in:
## EXPIRES CACHING ##
<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On
# Default directive
ExpiresDefault "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
# CSS
ExpiresByType text/css "access 1 month"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
<IfModule mod_headers.c>
SetEnvIf Origin "http(s)?://(www\.)?(www.simplymusicteachers.com/wp-admin/admin-ajax.php|www.simplymusicteachers.com)$" AccessControlAllowOrigin=$0$1
Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
</IfModule>
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## force HTTPS and www. if any of them are not already present
RewriteCond %{HTTP_HOST} (?!^www\.)^(.+)$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=302,L]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## force HTTPS and www. if any of them are not already present
RewriteCond %{HTTP_HOST} (?!^www\.)^(.+)$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=302,L]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
EDIT If for some reason lookbehind isn't supported try:
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=302,L]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.sub.domain.com%{REQUEST_URI} [R=302,L]