Easier access to another web app inside Symfony 4 - php

I have a Symfony 4 web app. Let's assume it is http://example.com.
I have installed HESK in /public/hesk/.
Now HESK is accessible via http://example.com/hesk/index.php, but when user skips "index.php", 404 error is shown.
I have tried return $this->redirect('...') and return new RedirectResponde('...') in my controller, but browser only says that there are too many redirects.
What is the correct way to redirect /hesk and /hesk/ to /hesk/index.php?

If you use Apache, you can put a .htaccess file in /public/hesk/ to force the redirect, something along those lines :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php$1 [L,QSA]
</IfModule>
Or use the DirectoryIndex index.php directive in your virtual host directly.
For Nginx, you need to modify your vhost to add the redirect. Something like that (not tested) :
location /hesk {
try_files $uri $uri/ /index.php$uri&$args;
}

Related

How can I force all URI requests to index.php?

In my index.php (which is in my project root directory) I have a basic routing system that grabs the url (using $_SERVER[‘REQUEST_URI]) and calls the proper ‘controller’ which then calls the proper ‘view’ (so, implementation of very basic MVC). What I have in my index.php looks like this:
$router= Router::load('Routes.php');
$uri=trim($_SERVER['REQUEST_URI'],'/');
require $router->direct($uri);
Problem is, I can not seem to make all requests go to index.php.
Obviously if you go to localhost/myproject/ you will get to index.php (therefore the proper controller will be called), however what I want is that even if you type localhost/myproject/contact to first be direct to index.php. So that index.php can handle routing for you.
If you are using apache you can use .htaccess file to redirect every request in your index.php file:
RewriteEngine On
RewriteRule ^/index\.php$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
If you are using nginx server you can redirect all request to index.php file with this code in your .conf file:
location / {
try_files $uri $uri/ /index.php;
}
Hope this helps you

User-Friendly Redirect If File Not Exist

I feel like this is a rather common request, but I am too confused about .htaccess and couldn't find a solution by Google.
I have a Laravel instance in a subdirectory of the Apache2 htdocs. Now I would like to invisibly redirect all requests from the root domain to this folder (it should be the "root" website). But the tricky thing is, this is not the only folder, there are other folders directly in the htdocs, which should be reached normally. Just the "root" website is not in the root but also in a subfolder. For example:
https://domainA.com should load https://domainA.com/laravel/public (including possible query string or parameters, but invisibly for the user)
https://domainA.com/websiteB should be served as it is
https://domainA.com/websiteC should be served as it is
...
I assume, part of this solution will be to list all the websiteB, websiteC directories in the .htaccess, would it be possible to automate this?
Thanks in advance!
You can put a .htaccess in the folder you want to custom controle but you have to create some filter condition
<IfModule mod_rewrite.c>
RewriteEngine On
## RewriteBase /foo
## conditions to tell what to redirect ie on URI
## RewriteCond %{REQUEST_URI} ^/a-folder/
## not websiteB or websiteC
RewriteCond %{REQUEST_URI} !^/websiteB/
RewriteCond %{REQUEST_URI} !^/websiteC/
## if the file does not exist call index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ my/path/to/a/script.php [L]
</IfModule>
After you have to do something special in script.php for those HTTP calls
You can also rewrite the URI and pass it again to apache but things can be complicated after...

Converting a rewrite from htaccess to nginx

I'm currently using this htaccess code in Apache to change the URL in address bar from http://www.domain.com/list?m=100 to http://www.domain.com/list/100
RewriteEngine On
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]*)$ index.php?m=$1 [QSA,L]
</IfModule>
The above htaccess is on /list directory.
I have tried to convert it to nginx (using http://winginx.com/en/htaccess), but I couldn't make it work. This is what I tried
location /list/ {
if (!-e $request_filename){
rewrite ^/([^.]*)$ /index.php?m=$1 break;
}
}
The above just downloads the PHP code. I also tried changing the break to last, but it just opens the homepage (the url in the address bar changes to http://www.domain.com/list/100). Any suggestions how I can make it work?
You don't need to convert everything, use this:
location /list/ {
rewrite ^/list/(.*)$ /index.php?m=$1 last;
}
Don't forget to restart nginx afterwards to see the changes.

.htaccess rules for pushstate support

I started an app based on Angular.js and Silex for the server side.
I would use real URL (without hash) but it's not working.
I have activate pushstate on angular with the following line :
$locationProvider.html5Mode(true);
but I don't know how to configure server. Actually when I try to access to localhost/test angular did nothing but silex said : *No route found for "GET /test" ..
my htaccess is like it :
<IfModule mod_rewrite.c>
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteBase /web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php [L]
Structure of my project is Like it
|--app/
|----app.php
|----controllers/
|-------MainController.php
|--web/
|----index.php
|----js/
|---------app.js
...
Thanks you for your help
edit :
route declaration :
config(['$routeProvider, $locationProvider', function($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider.when('/test',{
templateUrl : '/cuisine',
controller : 'CookController'
});
}
I am using this on my site, and its working , please give it try. Otherwise i suggest you turn on mod_rewrite logging in apache and look to logs
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteRule ^ /index.html
Your problem is with RewriteBase. The rewrite base is based on URI folder and you're requesting /test not /web/test, so your rewrite base should be /
I'm just guessing but I think your document root is /path/to/your/project/web (it should be this, only the web directory is meant to be public!) so your .htaccess shouldn't have the RewriteBase on your case.

301 redirect to same name file, same server, but differnet domain name

I recently did an overhaul of a website in which we switched to a new master domain name for that website. The old domain is still active, and needed to be redirected to the new domain name.
The site still has the same root folder, and so there are a couple of files that were updated, but have the same name (i.e. index.php, about.php, contact.php).
I manually redirected all the files (via .htaccess) that no longer exist to new files with a manual redirect:
redirect 301 /old_file.php http://www.new_domain.org/new_file.php
However this will not work with the files that have the same name such as "index.php". I can still call the site by the old domain name. How do I properly refer these files so that I don't have a duplicate penalty from google?
I tried this and it did not work:
#Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.old_domain.org$[OR]
RewriteCond %{HTTP_HOST} ^old_domain.org$
RewriteRule ^(.*)$ http://www.new_domain.org/$1 [R=301,L]
Thank You
Clear your browser cache and restart it
Make sure mod_rewrite and .htaccess are enabled through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Code
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?old_domain\.org$ [NC]
RewriteRule ^ http://www.new_domain.org%{REQUEST_URI} [R=301,L]
Take a look at using the canonical tag as an alternative.
http://www.seomoz.org/blog/cross-domain-canonical-the-new-301-whiteboard-friday

Categories