codeigniter rewriting of URLS not working - php

In the past, I have got this working no problems at all, but for some reason on my new server I just can't get it to work.
I have the following .htaccess file in the root of my application
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I have also enabled mod_rewrite and have confirmed this with php_info()
my site is located at /var/www/html/test
Is it possible that although mod_rewrite is enabled that it is not working and if so, how can I test it?

On some server implementations, you'll need to wrap it within <IfModule> tags.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Also check your httpd.conf file to ensure it has:
Options FollowSymLinks
AllowOverride All
It'd also be worth checking to makesure the .htaccess file isn't being overridden by another .htaccess file.
A simple way to test if .htaccess is working
Simply put the following in your .htaccess file:
deny from All
What this does is deny access to your site from everyone. If you are presented with a 403 forbidden when trying to access the site - the .htaccess file is being included. If not - see above.

I use this on my codeigniter setup
RewriteEngine on
# prevent these directories from hitting CI
RewriteCond $1 !^(index\.php|images|assets|robots\.txt|crossdomain\.xml)
# route everything else to the index.php
RewriteRule ^/(.*)$ /index.php/$1 [QSA]
(my setup is done in the virtualhost of .conf and not in .htaccess, though they are usually about the same configuration)

Related

.htaccess stop directory listing and forward to handler.php

I am trying to implement SEO friendly URLs using .htaccess and PHP.
I have achieved 90% of my goal and struggling with just one use case.
Here is what happing, when I access the following url
http://somesite.com/cms/movies/tarzan
it lands on
my-handler.php?the_url=movies/tarzan
This is perfect and that is what I want because then I manage it myself. The real problem is when I don't provide any slugs, then it lists the directory (means show all files and folders in it)
http://somesite.com/cms/
Can someone please help me fix following .htaccess content, so that even if I don't provide slug it should still be handled by my-handler.php instead of lisiting full directory?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /cms/
RewriteCond %{REQUEST_FILENAME} !/(admin|css|fonts|ico|include|js)/
RewriteRule ^my-handler\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /cms/my-handler.php?the_url=$1 [L,QSA]
</IfModule>
Solved
As per #Roman suggestion, I added the following to the above listing and it solved my problem. Plus I do not have to compromise on accessing physical directories RewriteCond %{REQUEST_FILENAME} !-d
DirectoryIndex my-handler.php
At first you have to set the DirectoryIndex to handle every request which points to / with your my-handler.php. This can be done by adding the line to your .htaccess
DirectoryIndex my-handler.php
To disable the directory listing, you have to forbid the listing by adding the following line to your .htaccess
Options -Indexes
Remember that this configuration is per .htaccess and just for the directory you are placing the file in. If you want to make changes for your whole webserver, you can edit the httpd.conf and search for
Options Indexes
and remove the Indexes option.
Documentation
The listing is provided by the mod_autoindex module.
Nice side-fact
If you just want to disable the listing of specific file-types like .env or .php files, you can add the option IndexIgnore *.php to you .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /cms/
RewriteCond %{REQUEST_FILENAME} !/(admin|css|fonts|ico|include|js)/
RewriteRule ^my-handler\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css|json|woff|ttf)
RewriteRule ^(.*)$ /cms/my-handler.php?the_url=$1 [L,QSA]
</IfModule>

htaccess stopped working after moving website to subdomain

So, I moved the website.com to subdomain.website.com. The .htaccess file is located in the website.com/subdomain/ folder, it should work but it's not working.
Error message: The requested URL /title-123 was not found on this server.
Basically what it does is translates the website.com/product.php?id=123&title=hello to website.com/hello-123
The error is produced when trying to access subdomain.website.com/title-123
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/product\.php\?id=([0-9]+)&title=([^\s&]+)\s [NC]
RewriteRule ^ %2-%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+?)-([0-9]+)$ product.php?id=$2&title=$1 [L]
</IfModule>
Anyone knows how to fix this? Thanks!
After a lot of research and debugging of apache conf files and receiving expert's assistance i managed to fix it removing completely the .htaccess file and adding the rewrite rules in the apache VirtualHost directly.

How to replace -MultiViews behaviour in htaccess?

I am trying to catch ALL requests to files in a directory and it's subdirectories and to route them to one single file in a subdirectory. Here is what the htaccess directives look like:
Options -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
# Excluding one directory
RewriteRule ^(excludedfolder)($|/).*\.(php)$ - [L]
# Redirect everything that is html or php to magicscript
RewriteRule \.(php|html|phps)$ /excludedfolder/magicscript.php [L]
RewriteRule ^/?$ /excludedfolder/magicscript.php [L]
RewriteRule ^/?index.php$ /excludedfolder/magicscript.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . excludedfolder/magicscript.php [L]
</IfModule>
This works on the mostpart of systems.
If i call someserver.com/testfile.php, it is properly routed to magicfile.php. If i call someserver.com/testfile, it also is properly routed to magicfile.php. The same for subfolders: If i call someserver.com/somefolder/testfile.php or someserver.com/somefolder/testfile, it is properly routed to magicfile.php.
But on some servers, it is not allowed to set the Options -MultiViews which causes the server to completely fail. If I remove the option, it still works, but it doesn't if I don't add the file extension.
How to redirect non existing files to magicfiles.php without having to use Options -MultiViews? Or how to detect if it is allowed to set the option in php?
Thanks for your help!
Maybe the server do not support Options -MultiViews http://httpd.apache.org/docs/2.2/mod/core.html#options

routing with htaccess and laravel

Hello I am using the following htaccess
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
For doing the laravel routing.
When I surf to example localdevurl/public/users I get the following error ( 404 )
Not Found
The requested URL /Users/username/Sites/sitefolder/public/index.php/user was not found on this server.
But as u see it shows the index.php in the error. When I put index.php in my url it does work indeed. I have turned on everything in my apache config.
httpd.conf
Can anyone see what I am doing wrong here?
In your .htaccess file, you are passing the request after the index.php part, instead of allowing laravel routing system to process it, this is:
RewriteRule ^(.*)$ index.php/$1 [L]
should be as the original .htaccess
RewriteRule ^ index.php [L]
I had the same problem with the index.php part, this is how I solved: There is a problem when you have userdir module enabled, which it seems to be the case based on the URL of the error message.
Possible solutions:
Create a symlink in the webserver folder to the public laravel project folder of the user web directory. e.g.: # ln -s ~/public_html/mylaravel4/site/public/ /var/www/mycoolsite
Then you can access http://localhost/mycoolsite
Replace RewriteCond and RewriteRule lines with this: FallbackResource /index.php
Make sure you have AllowOverride set to All instead of None for you particular vhost, and also set Order allow, deny rather than Order deny, allow in httpd.conf.
AllowOverride all
Order allow, deny

Can't find resources.json

For some reason when I deploy my API using Restler's API Explorer (a fork of Swagger UI) to production it gives me a general 404 error when I load:
/api/explorer
When I am more explicit and state:
/api/explorer/index.html
It loads the framing for the page but then reports "404 : Not Found ../resources.json" in red text below the header:
I'm fairly certain there's something environmental flaring up as the same files locally work. I also checked the .htaccess file in the /api/explorer directory and it looks right to me:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ index.html [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.html [QSA,L]
</IfModule>
Any help would be appreciated.
It turns out all the problems were down to a mod_rewrite problem. I'm still not 100% on WHY this problem showed up in one environment but not others with precisely the same httpd.conf and .htaccess. Oh well, the solution is to be explicit in the rewrite rule about your base url using the RewriteBase directive.
In the API directory I now have the following .htaccess file:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /api
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
In the API-Explorer directory I now have the following .htaccess:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /api/explorer
RewriteRule ^$ index.html [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.html [QSA,L]
</IfModule>
In order to troubleshoot this problem one invaluable tip that I came across is turning on Apache's mod_rewrite logging.
# Adding logging for Rewrites
RewriteLog logs/rewrite.log
RewriteLogLevel 2
Put this in any globally scoped area of httpd.conf; I put it around the entries that were already there about logging but you can also just put it at the end if you like that better. In addition, the basics around rewrite troubleshooting includes (apologies if this basic):
make sure that your httpd.conf has AllowOverride All and Options FollowSymLinks set for the directories you serving Restler out of.
You can put all of the configuration into httpd.conf and avoid .htaccess files altogether (and it's a bit faster that way too) but if you do that remember that httpd.conf has absolute url referencing versus .htaccess's relative.
You can check the ReadMe file on the Restler Github page
https://github.com/Luracast/Restler-API-Explorer#readme
Did you add the 'Luracast\Restler\Resources' class to create resources.json at API Root?
In your own index.php, the addAPIClass section should look like this:
$r = new Restler();
$r->addAPIClass('Luracast\\Restler\\Resources'); //this creates resources.json at API Root
// ... your own addApiClass
$r->handle();
It is in the documentation under "Use", but I had trouble figuring this out as well...
Make sure you have cache directory with write permission in your api root

Categories