Updating htaccess file with new contents gives errors - apache

I need to make a certain configuration to my apache in order to utilize a certain npm package https://github.com/tlaverdure/laravel-echo-server over cpanel. So I was wondering if it is possible to safely add the follow code to an existing .htacess file.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/socket.io [NC]
RewriteCond %{QUERY_STRING} transport=websocket [NC]
RewriteRule /(.*) ws://localhost:6001/$1 [P,L]
ProxyPass /socket.io http://localhost:6001/socket.io
ProxyPassReverse /socket.io http://localhost:6001/socket.io
Here are the contents of the .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
<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]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
# 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
</IfModule>
# php -- END cPanel-generated handler, do not edit
EDIT:
I do not have root access to place it within my /etc/httpd/conf/httpd.conf as a virtualhost.

I was able to resolve my problem by placing this within my .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?sub.domain\.com
RewriteRule ^socket.io/? http://localhost:6001/socket.io [R=301,L]

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...

Alternative procedure to prerender(not using prerender.io) in htaccess. Full custom code recommended

Is there any alternative procedure not to use prerender.io or custom way to implement it by writing code in htaccess? Need help. I am using angular 4 and apache server in back. Prerender.io asking for subscriptions.Please if anyone can help me writing the same custom code in htaccess.Here is my code in htaccess.
<IfModule mod_headers.c>
RequestHeader set X-Prerender-Token "xxxxxxxxxxxxxxxxxx"
</IfModule>
<IfModule mod_rewrite.c>
DirectoryIndex
RewriteEngine on
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Options +FollowSymLinks
#RewriteRule ^api/(.*)$ http://weedbuys.com/api/$1 [P,L]
# Prerender.io stuff
<IfModule mod_proxy_http.c>
RewriteCond %{HTTP_USER_AGENT} Baiduspider|DoCoMo|Twitterbot|
TweetmemeBot|Twikle|Netseer|Daumoa|SeznamBot|Ezooms|MSNBot|Exabot|MJ12bot|sogou\
sspider|bitlybot|ia_archiver|proximic|spbot|ChangeDetection|NaverBot|MetaJobBot|magpiecrawler|Genieo\sWeb\sfilter|Qualidator.com\sBot|Woko|Vagabondo|360Spider|ExB\
sLanguage\sCrawler|AddThis.com|aiHitBot|Spinn3r|BingPreview|GrapeshotCrawler|
CareerBot|ZumBot|ShopWiki|bixocrawler|uMBot|sistrix|linkdexbot|AhrefsBot|archive.
org_bot|SeoCheckBot|TurnitinBot|VoilaBot|SearchmetricsBot|Butterfly|
Yahoo!|Plukkie|yacybot|trendictionbot|UASlinkChecker|Blekkobot|Wotbox|YioopBot|
meanpathbot|TinEye|LuminateBot|FyberSpider|Infohelfer|linkdex.com|Curious\sGeorge|FetchGuess|ichiro|MojeekBot|SBSearch|WebThumbnail|socialbm_bot|SemrushBot|Vedma|alexa\ssite\
saudit|SEOkicks-Robot|Browsershots|BLEXBot|woriobot|AMZNKAssocBot|Speedy|oBot|
HostTracker|OpenWebSpider|WBSearchBot|FacebookExternalHit [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_
# Only proxy the request to Prerender if it's a request for HTML
RewriteRule ^(?!.*?(\.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))(.*) http://service.prerender.io/http://weedbuys.com/$2
[P,L]
</IfModule>
# Don't rewrite files or directories, but exclude adminer directory
RewriteRule ^(adminer)($|/) - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^adminer - [L,NC]
RewriteRule ^ index.html [L]
</IfModule>

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

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>