informatikus honlapja

htaccess 301 redirect

Category : apache, library · by jan 26th, 2014

htaccess Files Best Practice Guide With Examples and Functions

RewriteEngine On

# Redirect Entire two Site to New Domain

# 1.

RewriteCond %{HTTP_HOST} ^$ [OR]
RewriteCond %{HTTP_HOST} ^$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

# 2.

LoadModule rewrite_module modules/
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^localhost$ [NC]
  RewriteRule ^(.*)$$1 [R=301,L]

# Redirect Entire Site to New Domain
RewriteCond %{HTTP_HOST} ^$
RewriteRule ^/?$ "http\:\/\/new\.domain\.com\/" [R=301,L

# Redirect Entire Site to New Site

Redirect 301 /
Redirect 301 /old-page-name

# 301 Redirect Based on IP Address:

RewriteCond %{REMOTE_ADDR} ^(A\.B\.C\.D)$
RewriteRule ^/* [L]

# Non-www to www Redirect
# This is a very common function for redirecting your site
# at the top level. Sometimes a site can be accessed at
# and which can
# cause duplication and site management issues.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

# Clean up URLs to remove file extensions
# If you wanted to make your URLs look a little nicer by
# removing the file extension from the end of it you can
# use the function below. In order for this to work
# internally to the site you should also ensure that all
# the linking within the site has the extension manually
# removed.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

# Restrict access
# To add an element of security to the .htaccess file you
# should add the following function. It will deny access
# to anyone attempting to get to the file externally to
# view or change it. For example, if you were to add this
# to your file and then attempt to access it at
# you will be presented
# with a 403 forbidden error. This means its working!
<Files .htaccess>
order allow,deny
deny from all

(2) comments

Kerekes Peter
5 hónap ago · Válasz

Have sub directory not be password protected using Apache’s .htaccess

here is a way just using .htaccess. I saw this answer elsewhere

Just create an .htaccess file in the non-password protected subdirectory with the content:

Satisfy any

Kerekes Peter
5 hónap ago · Válasz

Create a new password file
# htpasswd -c /home/www/.htpasswd jerry
Change or update password
# htpasswd /home/www/.htpasswd -users tom
# vi /home/www/html/.htaccess
AuthType Basic
AuthName “Restricted Access”
AuthUserFile /home/www/.htpasswd
Require user vivek
# tail -f /var/log/httpd/access_log
# tail -f /var/log/httpd/error_log


Az email címet nem tesszük közzé. A kötelező mezőket * karakterrel jelöljük.

A következő HTML tag-ek és tulajdonságok használata engedélyezett: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>