Run Laravel from subfolder using apache Alias - apache

Im trying to run laravel from subfolder on my vps below is my configuration but it return too many redirect
apache alias
Alias /stockapp/ "/var/www/html/stockapp/public/"
<Directory "/var/www/html/stockapp/public/">
Order allow,deny
Allow from all
Require all granted
</Directory>
In public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /stockapp
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Related

Redirect an specific folder to a subdomain but without the folder name

Trying to do a rewrite for only the subfolder example.com/b to another domain.
example:
www.example.com/a/name --> doesn't redirect
www.example.com/b/name --> console.example.com/name 'note that the b is not present on the subdomain
How would it be the RewriteCond + RewriteRule / RewriteMatch command?
this what i have
RewriteCond %{REQUEST_URI} ^/b/?$
RewriteRule ([a-z0-9_-]*[\/]?)$ https://console.example.com/$1 [R=301,QSA,L]
Here is a copy of the current htaccess.conf file:
<Directory "/opt/bitnami/apps/wordpress/htdocs">
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)(tucita247\.com)$ [NC]
RewriteRule ^b/(.*)$ https://console.%1/$1 [R=301,NC,NE,L]
</Directory>
<Directory "/opt/bitnami/apps/wordpress/htdocs/wp-content/plugins/akismet">
# Only allow direct access to specific Web-available files.
# Apache 2.2
<IfModule !mod_authz_core.c>
Order Deny,Allow
Deny from all
</IfModule>
# Apache 2.4
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
# Akismet CSS and JS
<FilesMatch "^(form\.js|akismet\.js|akismet\.css)$">
<IfModule !mod_authz_core.c>
Allow from all
</IfModule>
<IfModule mod_authz_core.c>
Require all granted
</IfModule>
</FilesMatch>
# Akismet images
<FilesMatch "^logo-full-2x\.png$">
<IfModule !mod_authz_core.c>
Allow from all
</IfModule>
<IfModule mod_authz_core.c>
Require all granted
</IfModule>
</FilesMatch>
</Directory>
# BEGIN rlrssslReallySimpleSSL rsssl_version[4.0.6]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
# END rlrssslReallySimpleSSL
WORDPRESS htaccess
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Thanks!
You may use this redirect as your topmost rule in site root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\. [NC]
RewriteRule ^b/(.*)$ https://console.tucita247.com/$1 [R=302,NC,NE,L]
Make sure to clear your browser cache before testing this change.
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

Symfony 4 in subdirectory

I did install Symfony 4 on a subdirectory of my wamp localhost. My project is named "cardMaker".
I've a .htaccess file to redirect towards the public directory (index.php) :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule "^$" public/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . srcAngular/index.html [L]
</IfModule>
That works. But, when I open http://localhost/cardMaker/, I have this:
How can I set the base url ?
Here at https://symfony.com/doc/current/setup/web_server_configuration.html they say:
# If you run your Symfony application on a subpath of your document root, the
# regular expression must be changed accordingly:
# ProxyPassMatch ^/path-to-app/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/project/public/$1
Add the symfony app as an alias. Configure your apache httpd.conf file:
Alias /cardMaker "C:/Csi\CsiWorkspace/cardMaker/public"
<Directory "C:/Csi\CsiWorkspace/cardMaker/public">
AllowOverride All
Options Indexes FollowSymLinks
Require all granted
DirectoryIndex index.php index.html
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule cache/ - [F]
RewriteBase /symfonytest
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ ./index.php [L]
</IfModule>
</Directory>
Notice the use of RewriteBase for the alias cardMaker
This config (with php-fpm) currently works for me:
Apache: 2.4
Symfony: 3.4.44
<Directory /var/www/example.local>
Options Indexes FollowSymLinks
Allow from all
AllowOverride None
Require all granted
Order deny,allow
Deny from all
</Directory>
Alias /cardMaker /var/www/example.local/cardMaker/public
<Directory /var/www/example.local/cardMaker/public>
DirectoryIndex index.php
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
RewriteRule .* - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .+
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 307 ^/$ /index.php/
</IfModule>
</IfModule>
</Directory>
ProxyPassMatch ^/cardMaker(.*\.php(/.*)?)$ fcgi://php:9000/var/www/example.local/cardMaker/public$1

url not found error in laravel

I am using Laravel 5 with a new version of apache on ubuntu and when I try to access the page: localhost it works and if I try localhost/index.php/my_page it works but if I try localhost/my_page, it doesnt. So from searching other questions I guess my problem lies in the .htaccess file.
As per suggestions I saw online I added the rewriteBase and Options +FollowSymLinks to my .htaccess in laravel/public:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /repos/kenrose/public
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
and this is what my apache2.conf file looks
<Directory /repos/kenrose>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
I've taken all the suggestions i've found about this issue. What is the problem?
Try this:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Try to remove ur +FollowSymLinks part,hope it will work.
after running
a2enmod rewrite
in the command terminal, all is well.
I don't know what this does, or what the problem was though.

Deny from all folders except /index.php and two folders

This is an odd configuration, I'll give you that, but I'm looking to deny access to all folders in our system but it must be able to access,
/index.php (Wordpress bootloader)
/wp-*/* (e.g. wp-content, wp-admin, etc.)
/templates/* (some of our templates and custom content)
Everything else is denied (and there are hundreds of folders).
The problem I have is allowing index.php and 2 folders and then denying everything else inside /.
I should be able to accept,
http://example.com/
http://example.com/index.php
http://example.com/wp-content/...
http://example.com/wp-admin/...
http://example.com/wp-includes/...
http://example.com/templates/template.css
http://example.com/templates/subfolder/js/file.js
and reject,
http://example.com/thisIsAFolder
http://example.com/thisIsAnotherFolder
This is what I have,
<VirtualHost *:80>
ServerName example.com
DirectoryIndex index.php
DocumentRoot /var/www/
<Directory />
Order deny,allow
Deny from all
</Directory>
<Files index.php>
Allow from all
</Files>
<Directory "/var/www(/|/wp*/|/templates/)">
Allow from all
Options FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) wp/$2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ wp/$2 [L]
RewriteRule . index.php [L]
</Directory>
</VirtualHost>
But I continually receive Red Hat Enterprise Linux Test Page (using CentOS) and it allows for sub-directories because I allow /.
EDIT
This is the Apache config that ended up working for me. Big thanks to Jon Lin for the help.
<VirtualHost *:80>
ServerName example.com
DirectoryIndex index.php
DocumentRoot /var/www/
# first, deny all access
<Directory />
Order deny,allow
Deny from all
</Directory>
# then start to allow access where required
<Files index.php>
Allow from all
</Files>
<Directory "/var/www/">
Allow from all
Options FollowSymLinks
RewriteEngine On
RewriteBase /
# go directly to index.php if it is accessed
RewriteRule ^index\.php$ - [L]
# re-write URL for wp-admin access
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
# re-write wp-* access to route through wp/
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) wp/$2 [L]
# re-write all .php files to route through wp/
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ wp/$2 [L]
# go directly to real files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# respond to all other URLs by passing them through index.php
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
# deny access if URL doesn't start with these
RewriteCond %{REQUEST_URI} !^/(index\.php)?$
RewriteCond %{REQUEST_URI} !^/wp-[^/]+/
RewriteCond %{REQUEST_URI} !^/templates/
RewriteRule ^ - [L,F]
</Directory>
</VirtualHost>
You could try adding:
RewriteCond %{REQUEST_URI} !^/(index\.php)?$
RewriteCond %{REQUEST_URI} !^/wp-[^/]+/
RewriteCond %{REQUEST_URI} !^/templates/
RewriteRule ^ - [L,F]
right before:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
The problem here though, is that permalinks will probably break, because a request like: /posts/post-title/ will not match an of the "allowed" URI's. If that's a problem, then move it to right before this line:
RewriteRule . index.php [L]

mod rewrite not working with laravel and bitNami

http.conf has mod rewrite uncommented
so no custom routes are working someone in #laravel mentioned it would be because mod rewrite isn't working here is my setup:
laravel.conf has the following code:
Alias /laravel/ "C:\BitNami/frameworks/laravel/public/"
Alias /laravel "C:\BitNami/frameworks/laravel/public"
<Directory "C:\BitNami/frameworks/laravel/public">
Options +MultiViews
AllowOverride None
<IfVersion < 2.3 >
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.3>
Require all granted
</IfVersion>
</Directory>
If I uncomment these lines:
#RewriteEngine On
#RewriteRule ^/$ /laravel/ [PT]
then the main route will map to
http://localhost/
rather than
http://localhost/laravel
which is preferable but secondary to the main problem
.htaccess inside the public folder has this:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /laravel
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
here is my test code inside routes.php:
Route::get('test',function(){
return 'test worked';
});
which should resolve with
http://localhost/laravel/test
but instead I get a 404 error
RewriteBase and RewriteEngine are defined twice. Make your .htaccess file look like this:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /laravel
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
If possible, it would be best to use Apache virtual hosts instead.
Your .htaccess file should look like this.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)$ public/$1 [L]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>