redirecting homepage without affecting other rules - apache

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>

Related

.htaccess without matching ifmodule?

I am trying to deploy a laravel api with apache, but I am constant getting "internal server error".In errorlog, error is defined with "C:/Apache24/htdocs/ErenA/public/.htaccess: without matching section". I tried changing .htaccess file, delete the ifmodule element in httpd.conf and keeping only "directory index index.html index.php" but nothing seems to working
so here is my .htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
RewriteRule ^ index.php [L]
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
And Also I will add httpd.conf ifModule:
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
All AllowOverride is All and require all is granted in config file.
Can someone please help me?
You have 2 IfModule closing (</IfModule>), but only 1 opens <IfModule dir_module>
So your content should be like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
See that I have removed the middle </IfModule> and because you continue using Rewrite module (RewriteCond and RewriteRule), I just put those inside...

How to redirect users using .htaccess?

I am using Apache on my VM. I have this .htaccess right now.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Result
If I do
http://dips.awsri.com/
it works fine.
If I do
http://www.dips.awsri.com/
it redirects me to my log in screen, which I don't want 😓
http://www.dips.awsri.com/login
How can I stop that ? and instead redirect my user to
http://dips.awsri.com/

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

Codeigniter mod-rewrite in apache, removing index.php

So I am quite new to codeigniter and I have been trying to get my head around removing the index.php from my local site, I have managed to make it work for the homepage after looking through other similar questions on SO but none have got it fully working. Whenever I visit another page on my site it displays the wamp page on my screen. Here is my code in codeigniter/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
# !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
# slashes.
# If your page resides at
# http://www.example.com/mypage/test1
# then use
# RewriteBase /mypage/test1/
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
I have also put in my apache/conf/httpd.conf
<Directory />
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
But still no luck, is there something I've missed? Thanks in advance
Follow the following step:
1. Change the config file like this:
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
2.Use the following .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Note: .htaccess vary depending on server. I some server (e.g.: Godaddy) need to use the following .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Use this .htaccess works on most servers
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
# RewriteCond %{HTTPS} off
# RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
You can uncomment the last 2 lines to force HTTPS.
Also this could also work
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /foldername/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
Note line 3 of the 2nd .htaccess RewriteBase /foldername/ use this RewriteBase / if your script can be reached without including a folder name.

.htaccess rewrite - redirect pages

I would like to know how could I redirect all visitor who is accessing the following addresses:
https://example.com/anypage to https://example.com/search/anypage
It cannot redirect people who is already at https://example.com/search/anypage to avoid loop. This is my current .htaccess:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
<IfModule mod_php5.c>
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
# the following is for rewritting under FastCGI
<IfModule !mod_php5.c>
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Thanks
You can use this code in your DOCUMENT_ROOT/.htaccess file:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} !/search/ [NC]
RewriteRule ^(.*)$ search/$1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
<IfModule mod_php5.c>
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
# the following is for rewritting under FastCGI
<IfModule !mod_php5.c>
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>