htaccess for symfony2 in shared hosting - apache

i have a shared hosting.
I want to see a example of .htaccess file in public_html folder to setup a symfony2 application(the entry point is web/app.php)
Thanks a lot in advance

The symfony standard edition already contains a .htaccess which you can use.
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /app.php/
</IfModule>
</IfModule>

Try this:
DirectoryIndex public_html/app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public_html/
RewriteRule (.*) /public_html/$1
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
# When mod_rewrite is not available, we instruct a temporary redirect of
# the start page to the front controller explicitly so that the website
# and the generated links can still be used.
RedirectMatch 302 ^/$ /public_html/app.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>
Set it up on the root folder, one level above public_html.

Related

Updating htaccess file with new contents gives errors

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]

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>

Symfony .htaccess adding rule for app_MYNAME.php

i want to add another app_MYNAME.php in the web folder.
This file is nearly identical to the app.php.
The only difference is this entry:
$kernel = new AppKernel('MYKERNELNAME', false);
I want to load different configuration files with these method.
When i type in the browser:
domain.de/app_MYNAME.php/VALUE
all is fine.
Now i want to hide the app_MYNAME.php. The Result should look like:
domain.de/VALUE
The second step is the redirecting. When someone type in the browser:
domain.de/MYNAME/VALUE
it should be redirected to:
domain.de/app_MYNAME.php/VALUE
I only get a 404 or a misconfigured message.
At the end i need 3 app_MYNAME.php files (app_MYNAME1.php, app_MYNAME2.php, app_MYNAME3.php).
I hope someone could explain me, what to put in the .htaccess file.
Here is my .htaccess file:
DirectoryIndex app.php
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^ %{ENV:BASE}/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /app.php/
</IfModule>
</IfModule>
Try with a configuration that looks like this in your .htaccess file :
<IfModule mod_rewrite.c>
RewriteEngine On
# ... Your other stuff
# RewriteRule ^(.*)$ /app.php [QSA,L]
RewriteRule ^(.*)$ /app_MYNAME.php [QSA,L]
</IfModule>
Also, make sure that your nginx/apache host file is pointing to the web directory of your Symfony project.
Try this :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app_MYNAME.php [QSA,L]
Maybe all app.php could convert to app_MYNAME.php in your htaccess

Change public_html directory .htaccess

My folder for public_html is /domain.com/public_html/ but i want the htaccess to redirect them to the folder /domain/public_html/www/ but still have domain.com as domain and not domain.com/www .
EDIT:
I want the subfolder www in the public_html do be the default root and not public_html itself
Any solution on this?
Here is my current htacess
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ www/$1 [NC,L]
</IfModule>
If you can only go for .htaccess, this simple one should do it;
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/www/
RewriteRule ^(.*)$ /www/$1 [QSA]
Explanation;
RewriteEngine on # Turn on mod_rewrite functionality
RewriteCond %{REQUEST_URI} !^/www/ # Don't rewrite requests that are
# already to the /www/ directory.
# If this isn't here, we'll go into
# a neverending loop.
RewriteRule ^(.*)$ /www/$1 [QSA] # Rewrite all else to the www subdirectory
# QSA = keep the query string.
EDIT: Didn't see your existing htaccess, this (minus the redundant RewriteEngine statement) should probably go at the end instead of the whole IfModule block.
Try to put this in the htaccess on the public_html folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ www/$1 [NC,L]
</IfModule>
OR
Edit the apache configuration (httpd.conf)
vi /usr/local/apache/conf/httpd.conf
and set the DocumentRoot of the domain as
DocumentRoot /home/user/public_html/www
Save the file and restart the httpd service.
OR (recomanded)
Take a look at this question(and it's answer) - https://stackoverflow.com/a/5891858/1361042
EDIT
Use this HTACCESS:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/www/
RewriteRule ^(.*)$ www/$1 [NC, QSA]
</IfModule>
don't add it to yours but replace your htaccess with this one.

newbie trying to write url rewrite in .htaccess file

My site was under example.com/waha/.
Now I move the site right under the root example.com.
I want to 301 redirect all the old links like example.com/waha/notice/5803 to example.com/notice/5803.
How can I do it?
My current .htaccess file is below.
<IfModule mod_rewrite.c>
RewriteEngine On
# NOTE: change this to your actual StatusNet path; may be "/".
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?p=$1 [QSA,L]
</IfModule>
<FilesMatch "\.(ini)">
Order allow,deny
</FilesMatch>
Options -Indexes
Try this rule in front of your other mod_rewrite rule:
RewriteRule ^waha/(.*) /$1 [L,R=301]