.htaccess 301 redirect issue - apache

I need 301 redirect from example.com to www.example.com. This is working solution, but when
I paste it in whole .htaccess site go down.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Rest .htaccess file
php_flag magic_quotes_gpc off
php_flag magic_quotes_runtime off
php_flag register_globals off
RewriteEngine On
RewriteRule ^.htaccess$ - [F]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ $1 [L]
Options -Indexes
AddDefaultCharset windows-1251
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(service|news|search|project)/?$ index.php?menu=$1
RewriteRule ^error/([0-9]+)/?$ index.php?menu=error&loadinfo=$1
RewriteRule ^page/([0-9]+)/?$ index.php?menu=page&loadinfo=$1
RewriteRule ^news/([0-9]+)/?$ index.php?menu=news&loadinfo=$1
RewriteRule ^news/page-([0-9]+)/?$ index.php?menu=news&page=$1
RewriteRule ^service/([0-9]+)/?$ index.php?menu=service&category=$1
RewriteRule ^service/([0-9]+)/([0-9]+)/?$ index.php?menu=service&category=$1&loadinfo=$2
RewriteRule ^project/([0-9]+)/?$ index.php?menu=project&loadinfo=$1

Whats the AllowOverride value of the folder where you have your .htaccess? Your DocumentRoot (or VirtualHost) specification needs to be set to at least AllowOverride Options in order to allow php_flag within .htaccess files.
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Related

Apache: client denied by server configuration, and I cant find the error

Im taking a project in my company where noneone knows about that, its a legacy code and noneones know where my error.
this project its based in SilverStripe 3.1.12 and AngularJS 1.XX
When i tray to create a vagrant machine for this proyect, I can not access to him using the brower, I always recevie
Forbidden
You don't have permission to access / on this server.
And in the logs
[Thu Sep 07 11:44:49.485143 2017] [authz_core:error] [pid 29040:tid 140301297456896] [client 192.167.1.1:55872] AH01630: client denied by server configuration: /vagrant/
the apache .conf file
<VirtualHost *:80>
ServerName mysiteonline.dev
DocumentRoot "/vagrant/"
ErrorLog /home/ubuntu/mysite-error.log
CustomLog /home/ubuntu/mysite-access.log combined
</VirtualHost>
The proyect has 2 .httaccess file
.
├── .httaccess (1)
├-- mysite-app
├-- .httaccess (2)
The fristone handle the silverstripe part, the secondne its for the angular part.
httacess for silverstripe:
DirectoryIndex index.php
# Deny access to templates (but allow from localhost)
<Files *.ss>
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Files>
# Deny access to IIS configuration
<Files web.config>
Order deny,allow
Deny from all
</Files>
# Deny access to YAML configuration files which might include sensitive information
<Files *.yml>
Order allow,deny
Deny from all
</Files>
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf|json)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>
# Route errors to static pages automatically generated by SilverStripe
ErrorDocument 404 /assets/error-404.html
ErrorDocument 500 /assets/error-500.html
<IfModule mod_rewrite.c>
SetEnv HTTP_MOD_REWRITE On
RewriteEngine On
RewriteBase '/'
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^nl/(game|category|info)/[0-9]*-(.*)$ https://mysitespelletjes.nl/$2 [NC,R=301,L]
RewriteRule ^nl/(game|category|info)/*$ https://mysitespelletjes.nl/$1 [NC,R=301,L]
RewriteRule ^nl/(.*)$ https://mysitespelletjes.nl/$1 [NC,R=301,L]
RewriteRule ^nl$ https://mysitespelletjes.nl [NC,R=301,L]
RewriteRule ^en/(game|category|info)/[0-9]*-(.*)$ https://mysiteonline.com/$2 [NC,R=301,L]
RewriteRule ^en/(game|category|info)/*$ https://mysiteonline.com/$1 [NC,R=301,L]
RewriteRule ^en/(.*)$ https://mysiteonline.com/$1 [NC,R=301,L]
RewriteRule ^en$ https://mysiteonline.com [NC,R=301,L]
RewriteCond %{REQUEST_URI} !(\.css|\.js)$ [NC]
RewriteRule ^bower_components/(.*)$ mysite-app/bower_components/$1 [L]
RewriteRule ^bower_components/(.*)\.(css) mysite-app/bower_components/$1.css [L,T=text/css]
RewriteRule ^bower_components/(.*)\.(js) mysite-app/bower_components/$1.js [L,T=application/javascript]
RewriteCond %{REQUEST_URI} !(\.css|\.js)$ [NC]
RewriteRule ^app/(.*)$ mysite-app/app/$1 [L]
RewriteRule ^app/(.*)\.(css)$ mysite-app/app/$1.css [L,T=text/css]
RewriteRule ^app/(.*)\.(js)$ mysite-app/app/$1.js [L,T=application/javascript]
RewriteCond %{REQUEST_URI} !(\.css|\.js)$ [NC]
RewriteRule ^lib/(.*)$ mysite-app/lib/$1 [L]
RewriteRule ^lib/(.*)\.(css)$ mysite-app/lib/$1.css [L,T=text/css]
RewriteRule ^lib/(.*)\.(js)$ mysite-app/lib/$1.js [L,T=application/javascript]
RewriteCond %{REQUEST_URI} !(\.css|\.js)$ [NC]
RewriteRule ^dist/(.*)$ mysite-app/dist/$1 [L]
RewriteRule ^dist/(.*)\.(css)$ mysite-app/dist/$1.css [L,T=text/css]
RewriteRule ^dist/(.*)\.(js)$ mysite-app/dist/$1.js [L,T=application/javascript]
RewriteRule ^terms(.*) / [L]
RewriteRule ^vendor(/|$) - [F,L,NC]
RewriteRule silverstripe-cache(/|$) - [F,L,NC]
RewriteRule composer\.(json|lock) - [F,L,NC]
RewriteRule ^lp($|/) - [L]
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/framework/main.php !-f
RewriteRule .* - [S=14] #If the above conditions fail skip all silverstripe routes.
RewriteRule ^Security/(.*) framework/main.php?url=Security/$1 [QSA,L]
RewriteRule ^admin(.*) framework/main.php?url=admin$1 [QSA,L]
RewriteRule ^block-account/(.*)/ framework/main.php?url=block-account/$1 [QSA,L]
RewriteRule ^affiliate/ framework/main.php?url=affiliate/ [QSA,L]
RewriteRule ^mediatek-proxy/(.*) framework/main.php?url=mediatek-proxy/$1 [QSA,L]
RewriteRule ^api/(.*) framework/main.php?url=api/$1 [QSA,L]
RewriteRule ^registration-api/(.*) framework/main.php?url=registration-api/$1 [QSA,L]
RewriteRule ^verify/(.*) framework/main.php?url=verify/$1 [QSA,L]
RewriteRule ^forgotpassword/(.*) framework/main.php?url=forgotpassword/$1 [QSA,L]
RewriteRule ^adyen(.*) framework/main.php?url=adyen/$1 [QSA,L]
RewriteRule ^providers/(.*) framework/main.php?url=providers/$1 [QSA,L]
RewriteRule ^incompatible-browsers framework/main.php?url=incompatible-browsers/ [QSA,L]
RewriteRule ^sitemap-generator framework/main.php?url=sitemap.xml [QSA,L]
RewriteRule ^dev(.*) framework/main.php?url=dev/$1 [QSA,L]
#IMAGES
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^image/(.*)\.png$ framework/main.php?url=image/$1 [QSA,L]
#PRERENDER
RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_
RewriteCond %{QUERY_STRING} !url
RewriteCond %{REQUEST_URI} !^/image
RewriteRule ^(?!.*?(\.woff|\.ttf|\.svg|\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent))(index\.php)?(.*) mysite-app/$3?_escaped_fragment_= [L]
#SITEMAP.xml
RewriteRule ^(.*)sitemap.xml mysite-app/sitemap.xml [L]
#MYSITE-APP pass through
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* mysite-app/ [L]
</IfModule>
And for Angular part
DirectoryIndex index.php
<IfModule mod_headers.c>
# Change YOUR_TOKEN to your prerender token and uncomment that line if you want to cache urls and view crawl stats
RequestHeader set X-Prerender-Token "LH4zvWkRZ0oGgbIgoAuw"
</IfModule>
<IfModule mod_rewrite.c>
SetEnv HTTP_MOD_REWRITE On
RewriteEngine On
RewriteBase '/'
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# With the below proxy we achieve that sitemap.xml is proxied through our main server. And that the main server is informed from which URL the request was made
# This always the main server to server the sitemap.xml based on that domain.
# RewriteRule ^(.*)sitemap.xml http://mysite.local/sitemap-generator/?referrer=%{HTTP_HOST}/$1 [P,L]
<IfModule mod_proxy_http.c>
RewriteRule ^(.*)sitemap.xml https://mysiteonline.com/sitemap-generator/?referrer=%{HTTP_HOST}/$1 [P,L]
#RewriteRule ^(.*)sitemap.xml http://mysite.local/sitemap-generator/?referrer=%{HTTP_HOST}/$1 [P,L]
</IfModule>
# Prerender IO configuration
# Change the below url to the http://service.prerender.io/ server for staging + live else point it to http://localhost:3000/ DO NOT FORGET THE TRAILING SLASH!!!
# LIVE = http://service.prerender.io/
# DEV = http://localhost:3000/ MAKE SURE TO DISABLE AUTH!!
<IfModule mod_proxy_http.c>
RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_
RewriteCond %{QUERY_STRING} !url
RewriteCond %{REQUEST_URI} !^/image
RewriteRule ^(?!.*?(\.woff|\.ttf|\.svg|\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent))(index\.php)?(.*) https://service.prerender.io/https://%{HTTP_HOST}/$3 [P,L]
#RewriteRule ^(?!.*?(\.woff|\.ttf|\.svg|\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent))(index\.php)?(.*) http://localhost:3000/http://%{HTTP_HOST}/$3 [P,L]
</IfModule>
</IfModule>
Please i have 2 days fighting, i cant not find the solution, for this... all the time I get:
Forbidden
You don't have permission to access / on this server.
You are still using old Apache 2.2 syntax which does not work anymore in Apache 2.4 (which I assume you are using).
The lines
Order deny,allow
Deny from all
Allow from 127.0.0.1
don't work anymore.
You must add a Require line:
Require local
For a full documentation on how to upgrade you Allow from/Deny from clauses see this document:
https://httpd.apache.org/docs/current/upgrading.html

redirecting homepage without affecting other rules

My current .htaccess is given below.
# Make sure directory listing is disabled
Options +FollowSymLinks -Indexes
RewriteEngine on
# Send request via index.php (if it's not an existing file/folder)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
<IfModule mod_php5.c>
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_php5.c>
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
I want to redirect my homepage to another domain, but maintain all the /sub/directories.
So changed the rules as below.
# Make sure directory listing is disabled
Options +FollowSymLinks -Indexes
RewriteEngine on
# Send request via index.php (if it's not an existing file/folder)
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#redirect
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://example.com/ [L,R=301]
<IfModule mod_php5.c>
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_php5.c>
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Now all the /sub/directories are getting internel server error. Logs suggesting it's going on loops.
Any idea?
RewriteCond is only for the next RewriteRule.
Use that:
# Make sure directory listing is disabled
Options +FollowSymLinks -Indexes
RewriteEngine on
#redirect
RewriteRule ^$ http://example.com/ [L,R=301]
# Send request via index.php (if it's not an existing file/folder)
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
<IfModule mod_php5.c>
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_php5.c>
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

Mod Rewrite generating 500 internal error on page

For some reason my .HTACCESS file won't re-write my users page, even though I'm sure the code to re-write it is correct.
I want to turn
http://www.tfreeb.com/user/user?user=duenna
Into
http://www.tfreeb.com/user/duenna
And this is my HTACCESS file, could it be that something else is conflicting with it?
The re-write rule for the user page is right at the bottom.
Options -Indexes
php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on
php_flag log_errors on
php_value error_log /home/path/public_html/domain/PHP_errors.log
php_value error_reporting 32767
ErrorDocument 404 /notfound.html
ErrorDocument 403 /403.html
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.tfreeb.com$ [NC]
RewriteRule ^(.*)$ http://www.tfreeb.com/$1 [L,R=301]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]
RewriteCond %{HTTP_HOST} ^213\.143\.20\.122
RewriteRule (.*) http://www.tfreeb.com/$1 [R=301,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteEngine On
RewriteRule ^user/([^/]*)$ /user/user.php?user=$1 [L]
After:
RewriteRule (.*) http://www.tfreeb.com/$1 [R=301,L]
Try with that:
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^user/([^/]*)$ /user/user.php?user=$1 [NC,L]
RewriteRule ^([^.]+)$ $1.php [NC,L]
No need to use multiple times RewriteEngine On
Are you sure that the .htaccess is being read?
I'd try to make a basic redirect to some page, p.e. google.com if this doesn't work you should ask to the web server admin to give you the AllowOverride = all directive for the directory.
p.e.
<directory /var/www/mysite>
AllowOverride = all
</directory>

Redirect entire directory with htaccess

I can't figure out why my redirects aren't working as they are in another htaccess file.
I'm trying to redirect old urls to new ones.
RewriteEngine on
AddDefaultCharset UTF-8
DirectoryIndex index.php index.html
<IfModule mod_php.c>
php_flag magic_quotes_gpc off
</IfModule>
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule (.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
Redirect /private_3 http://website.com/new_private
I've tried all sorts of things that I've found on the web but nothing works.
Please help.
Thanks.
Instead of using this:
Redirect /private_3 http://website.com/new_private
Try adding this right below the RewriteEngine on line:
RewriteRule ^private_3(.*)$ http://website.com/new_private$1 [L,R=301]
Looks bad ordering and mixing of mod_alias with mod_rewrite might be a problem here. Try reordering your rules as this:
AddDefaultCharset UTF-8
DirectoryIndex index.php index.html
<IfModule mod_php.c>
php_flag magic_quotes_gpc off
</IfModule>
RewriteEngine on
RewriteRule ^private_3 http://website.com/new_private [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
RewriteRule ^(system.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1

Mod_rewrite and subdirectory access

I'm trying to deny access to couple of subdirectories (when using specific url) that are in www-root.
My goal is:
Make domain.com/subdir{1-3} to deny access.
Make subdir{1-3}.domain.com to point to %{DOCUMENT_ROOT}/subdir{1-3}. Already works.
I want to write rules to httpd.conf without having to use .htaccess files and use only one Directory section if possible.
Here's what I've tried:
<Directory "/var/www/subdir1">
<IfModule rewrite_module>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/subdir1 [NC]
RewriteRule ^.* - [F]
</IfModule>
</Directory>
But no go.
//E
Got it working, here's what I did:
<Directory />
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/subdir1/ [OR]
RewriteCond %{REQUEST_URI} ^/subdir2/ [OR]
RewriteCond %{REQUEST_URI} ^/subdir3/ [OR]
RewriteCond %{REQUEST_URI} ^/subdir4/
RewriteRule .* - [F]
</IfModule>
</Directory>
Is there anything I should change to make it more safe?
Everything works except subdir3 which points to ownclouds root dir. It must have something to do with ownclouds own rewrite rules:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule ^.well-known/host-meta /public.php?service=host-meta [QSA,L]
RewriteRule ^.well-known/host-meta.json /public.php?service=host-meta-json [QSA,L]
RewriteRule ^.well-known/carddav /remote.php/carddav/ [R]
RewriteRule ^.well-known/caldav /remote.php/caldav/ [R]
RewriteRule ^apps/calendar/caldav.php remote.php/caldav/ [QSA,L]
RewriteRule ^apps/contacts/carddav.php remote.php/carddav/ [QSA,L]
RewriteRule ^apps/([^/]*)/(.*\.(css|php))$ index.php?app=$1&getfile=$2 [QSA,L]
RewriteRule ^remote/(.*) remote.php [QSA,L]
</IfModule>
Tips are welcome.
Try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^subdir1 - [L,F]
RewriteCond %{HTTP_HOST} ^subdir1\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subdir1/
RewriteRule ^(.*)$ /subdir1/$1 [L]
for "subdir1".