Setting up ModRewrite for Silex - issue with Routing - php

I've set up quite a few Silex/Symfony apps, without ever having problems. I now need to work on a shared domain only with htaccess capability.
Following setup on the Silex documentation, along with browsing around, I set up my .htaccess as follows:
(http://silex.sensiolabs.org/doc/web_servers.html)
Website looks like http://example.com/webpath/to/silex/. This file is in /drive/path/to/silex/.htaccess
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /webpath/to/silex/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)? web/index_dev.php/$1 [QSA,L]
</IfModule>
This seems to work well, aside from all routing includes the /webpath/to/silex/. IE. Continual NotFoundHttpException: No route found for "GET /webpath/to/silex/home" errors.
My experience with mod_rewrite has been simple and limited; I've spent a while playing around with permatations of the Rule and the Base without any luck.
How can I pass just the end of the URL to the silex app?

Not sure, but check that...
mode_rewrite is allowed (using Bash) :
$ sudo a2enmod rewrite
$ sudo service apache2 restart
you've got a sensible & existing base path value: Is "/webpath/to/silex/" really correct ? Just comment this line with a #, if it's useless to you

Related

There is "/?i=2" in my wordpress website index url

I recently grabbed a host and first there was an index2.html in my htdocs root which was default index, I didn't remove that until I install wordpress in that root. then wordpress automatically made a httaccess file with following codes which is normal:
# BEGIN WordPress
<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
now I realize that sometimes there is /?i=1 or /?i=2 in my general url, for example, wordpresssite.com/?i=2
this is a problem because I'm using some ad-unit system and I gave my website url to the system but it seems that the ad system bots some time redirect to "wordpresswebsite.com/?i=2" url and it does not match with the url I entered before (wordpresswebsite.com) so it's not gonna work.
now I removed the index2.html but the problem is still there.
this ?i=1 or 2 is about PHP GET parameter but how this parameter will appear.
I found this on reddit with similar issue and the conclusion was:
Most likely there are some rules at vhost level.
---- yes it's from the host, they said they cannot disable it as it's protecting me from bots.
For those interested in removing the /?i=1 installed mostly by free host providers here's a tip.
Edit & upload the .htaccess file in root of website
RewriteCond %{QUERY_STRING} ^(.*)\?i=1$ [NC]
RewriteRule ^(.*)$ /$1?%1 [R=301, L]
If you're using Apache, you have to enable mod_rewrite first.
sudo a2enmod mod_rewrite
Then restart or reload your server for the new configuration to take place:
sudo service apache2 restart
(Try to find some fancy control panel option for this, if not having shell access)
?i=1 or 2 or 3 is to protect your website from bots. Try using cloudflare for your website or use the .htaccess code to remove ? paramaters. Note that using any code to remove ?i=1 will remove all ? parameters.

Laravel Localhost or pretty url not working

I have installed composer on windows 8 and i am using the webserver xampp,
I did the comand composer global require
composer global require "laravel/installer=~1.1"
I even configure env path for composer like this
set PATH=%PATH%;%USERPROFILE%\AppData\Roaming\Composer\vendor\bin
Then created the project like this
Laravel new myproject
I check the file .htaccess in my public folder of my laravel project,
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
I have checked the module activation for url rewriting in apache httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so
then i test it and i typed many urls and it gives a page not found ERROR !
Here are the url tested:
localhost/laravel/
localhost/laravel/public
localhost/laravel/myproject/
localhost/laravel/myproject/public
Did i miss something, i am quite surprised to fix things for hours to use a popular framework like laravel.
From the comment of my question we conclude that laravel has a bug with xampp and the bug was reported 3 days ago on the official bug tracking forum from laravel
Please check this link: http://github.com/laravel/framework/issues/8129
Also we have found that my folder structure is too long and it could generate conflict within the configuration of xampp, the advice was to use a virtual host solution, the one advised by laravel is Homestead, so let's try with homestead !
http://laravel.com/docs/5.0/homestead
Good luck to everyone who need to fix their xampp server to work with laravel maybe try to use another webserver instead.

Symfony2 - How can I remove 'web/app_dev.php/' from my url?

How can I remove 'web/app_dev.php/' from my url when I want my symfony website to go live?
This is the default url during development,
http://{localhost}/my-symfony-2/web/app_dev.php/hello/World
So when I go live, I would like to be able to use this url to access my symfony website,
http://my-symfony-2.com/hello/World
Any ideas?
To hide app.php you need to:
having access at least to the web root of your site. Usually on the control panel of your web hosting space you can find from which you can upload your files (or if you have the access credentials you can install and use a Free FTP client like Filezilla).
checking if you have the mod_rewrite module installed and enabled in Apache looking the phpinfo() under "apache2handler" ---> "Loaded Modules" directory (you should have that possibility directly through the control panel).
After these checks you have to:
NOTE: Symfony2 already comes with an .htaccess file stored in the default web directory but if you don't know what are you doing it's better to replace the directives containet within the "IfModule mod_rewrite.c" with those shown below.
Use the native Symfony2 .htaccess or create a new one file with the .htaccess extension and copy/paste inside these directives to hide app.php in production (not on localhost):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{CONTEXT_PREFIX}/$2 [R=301,L]
RewriteRule .? - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ app.php [QSA,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule .? %{ENV:BASE}app.php [L]
</IfModule>
# and from the symfony's original
<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 ^/$ /app.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>
To apply the modifications you need to restart Apache (also this is usually did on the control panel of your web space).
Googling a bit you can find tons of examples and tutorials but to start to learn take a look at this simple .htaccess guide with many useful infos and examples and the Official URL Rewriting Guide (Apache2.4). If you encounter some problem update your post adding all related infos otherwise you can make another question here on SO.
If you have access to the deployment server, from terminal you can use:
php app/console cache:clear --env=prod and this will clear the prod cache and you will be able to use the standard route.
But if you are deploying the application on hosting where you haven't access to terminal, need to find the app/cache/* and app/logs/* and remove their content and after add the proper rights (766|777) for the folders.

Laravel in subfolder on webhost

I have a webapp written in Laravel that needs to run in a folder on a web host.
The app will have to be accessible via hostname.com/webhit/. This will point to the app's home page.
I only have one route:
Route::controller('/', 'HomeController');
HomeController's getIndex needs to serve the home page. This works.
However, as soon as I want to go to something like hostname.com/webhit/login, I get a 404 from Apache.
Obviously, .htaccess is not working properly. I need it to, essentially, turn URLs that look like hostname.com/webhit/login into hostname.com/webhit/index.php/login.
I have a .htaccess file in www/webhit (where index.php is located) that looks like this:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ webhit/index.php/$1 [L]
</IfModule>
I am not very familiar with .htaccess file syntax, but I believe it's doing something wrong.
Edit:
I did it. My .htaccess was wrong (it actually causes a redirect loop), but the issue was that it wasn't even being parsed by Apache (hence the 404 instead of a 500 due to >10 redirects in a request). I did the following steps in order to get everything to work:
Enable mod_rewrite and restart Apache (plenty of docs out there on how to do this)
But wait, there's more! By default, Apache on Ubuntu prevents URL rewrites. See this site. Most importantly, the following fragment from the URL above is very important: "By default, Ubuntu's Apache will ignore the directives in your .htaccess files." You will need to actually enable rewrites by editing \etc\apache2\sites-available\default and setting AllowOverride to all (see link above for more details).
Reload the configuration (or just restart apache).
Make sure you're using the correct .htaccess. My original version actually has a redirect loop in it. See the selected response for the correct version.
After this, I got it to work. I hope it helps future programmers having a similar issue!
Check default server requirements - laravel .htaccess file works for most situations. Try with this:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I would suggest you to use resourceful controllers - mappings from your route to your controllers methods are much more clear, and you'll get full
resource with one command (routes,models,views,controllers)

Clean URLs don’t work after upgrade

I recently upgraded my CodeIgniter app from v1.7.2 to 2.0.2.
I use mod_rewrite to produce clean URLs without index.php e.g. example.com/controller/action. It is working on my local dev server but when I updated my production app all requests to example.com/controller/action result in displaying main page (i.e. index.php).
When I access address like that: example.com/index.php/controller/action it works.
Mod_rewrite is on as it was working fine with previous code version. With 1.7.2 I used $config['uri_protocol'] = "AUTO" but now I switched to "PATH_INFO" because auto resulted in 404.
Because it is working on dev I’m wondering if I missed any deployment step. Any ideas?
There are .htaccess rules I'm using with CI 2:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
<IfModule mod_rewrite.c>
#paths that you don't want to rewrite
RewriteCond $1 !^(index\.php|assets|session|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
I think I had some similar problem and I believe it got resolved by changing permission on my root folder to 777... but that was on XAMPP

Categories