Error with "RewriteEngine On" in .htaccess file on web server - php

RewriteEngine On
RewriteRule ^bloco/(.*)$ bloco.php?id=$1
RewriteRule ^bloco/(.*)/$ bloco.php?id=$1
My .htaccess file have that configuration, work well on my local server but at web server dont work...
local:
domain.com/bloco/590
web server:
domain.com/bloco/?id=590
right now (not working...)
Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteEngine On
RewriteRule ^bloco/(.*)(/?)$ /carnaval/blocosderua/bloco.php?id=$1 [NC,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1\.php
php_value allow_url_fopen 1
php_value allow_url_include 1
any help?

Modify your RewriteRule to look like this:
RewriteEngine On
RewriteRule ^bloco/(.*)(/?)$ /bloco.php?id=$1 [NC,L]
This assumes bloco.php is in your root. If it is not, you will need to specify the path to it:
RewriteRule ^bloco/(.*)(/?)$ /path/to/bloco.php?id=$1 [NC,L]

Not all hosting companies allow you to use the rewrite engine. If your getting an error try to see if it also does with only the RewriteEngine On rule. If it does I'm afraid your not allowed to use it.
As for the rules they look fine to me.
Greets Michael

You probably need to include Options +FollowSymLinks at the top of your .htaccess. Otherwise this will need to be configured in your httpd.conf.
This will basically allow you to override configuration values in your .htaccess.

Related

GET Variable lost on RewriteRule

I have written a redirect to change quiz.php?quiz=1 to /quiz/1 however PHP can no longer pickup the GET Variables, is there anything i'm missing? This is my htaccess file:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^quiz/([^/]*)$ /quiz.php?quiz=/$1 [QSA,L]
You have error in RewriteRule ^quiz/([^/]*)$ /quiz.php?quiz=/$1 [QSA,L]
There should not be slash at redirect target, so correct is RewriteRule ^quiz/([^/]*)$ /quiz.php?quiz=$1 [QSA,L]
I tried this on localhost and it is working properly. Try debugging via e.g. var_dump($_GET);
In this specific case, you probably need to disable content negociation and AcceptPathInfo features by adding the following lines to your .htaccess file:
AcceptPathInfo off
Options -MultiViews
Content negociation can internally "rewrite" "quiz" into "quiz.php". And AcceptPathInfo "quiz.php/a/b/c" to "quiz.php" with $_SERVER['PATH_INFO'] = '/a/bc/c'. They both happen before rewriting, bypassing your rule.

codeigniter rewriting of URLS not working

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)

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.

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

Apache Rewrite URL, Works in WAMP, not in LAMP

I have a custom MVC app, where in it used IndexController naming convention, which I have built on WAMP. Today i tried it to put in to Linux (LAMP).. Strangely, it is giving error "page not found". Can anybody help. I am not good at mod rewrite,
Following is the code
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=$1 [L,QSA]
URL is
http://hostname/mvc/incident/add
Error is The requested URL /app01/users/public_html/mvc/index.php was not found on this server.
You need to put a / before index.php and some other stuff. Look at the example I got from Wordpress's one.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Try either adding:
RewriteBase /mvc/
just under the RewriteEngine on directive, or add a leading slash to index.php:
RewriteRule ^(.*)$ /mvc/index.php?request=$1 [L,QSA]
Hi I solved this issue by using error redirecting functionality. I put following code in my .htaccess file
ErrorDocument 403 /~renjith/mvc/index.php
ErrorDocument 404 /~renjith/mvc/index.php
and in index.php file i used, $_SERVER['REQUEST_URI'] to get thr URL with query strings
I think that ./htaccess configuration is correct. I solved it simply by adding this directive to my apache2.conf file on debian.
<Directory /var/www/html/your_app_folder>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted</Directory>

Categories