Apache Rewrite URL, Works in WAMP, not in LAMP - php

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>

Related

Make a friendly URL with 2 parameters on PHP with Apache

So I am in a bit of a soup here.
I need to make a friendly URL.
This is the URL where I enter the search values project_name and version
localhost:8080/demo_webpages_project/retrieval.html
This is where it takes me to on submitting the values
localhost:8080/demo_webpages_project/download.php?project_name=QT&version=3.1.2
I want the URL in the following form
localhost:8080/demo_webpages_project/download/QT/3.1.2
I tried doing this using .htaccess but not really finding a solution
The following is the code for the .htaccess file.
<IfModule mod_rewrite.c>
#Turn the engine on
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#attempt1
#RewriteRule ^(.*)/$ download.php?project_name=$1&version=$1
#RewriteRule ^/(.*)$ download.php?version=$1
#attempt2
#RewriteRule ^(.*)/(.*)$ download.php?project_name=$1&version=$2 [L,NC,QSA]
#RewriteRule ^(.*)$ download.php?version=$1
</IfModule>
I would be really grateful if someone could help me out with this. This is the first time I am working on something on this and am lost.
Thanks
Have this .htaccess inside /demo_webpages_project/:
Options -MultiViews
RewriteEngine on
RewriteBase /demo_webpages_project/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ download.php?project_name=$1&version=$2 [L,QSA]
According to this article, you want a mod_rewrite (placed in an .htaccess file) rule that looks something like this:
RewriteEngine on
RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1
And this maps requests from
/news.php?news_id=63
to
/news/63.html
Another possibility is doing it with forcetype, which forces anything down a particular path to use php to eval the content. So, in your .htaccess file, put the following:
<Files news>
ForceType application/x-httpd-php
</Files>
And then the index.php can take action based on the $_SERVER['PATH_INFO'] variable:
<?php
echo $_SERVER['PATH_INFO'];
// outputs '/63.html'
?>

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 render a simple page when using FatFree and PHP5.3 on Apache 2.2

I am using Apache 2.2 and PHP 5.3. I am trying to use Fatfree framework for routing. My index.php file looks like:
<?php
require_once 'f3/lib/base.php';
F3::route('GET /','home');
function home() {
echo F3::render('templates/index.html');
}
F3::route('GET /#pagenum','mainlist');
function mainlist() {
F3::set('pagenum', #pagenum);
echo Template::serve('templates/index.html');
}
F3::run();
?>
If I go to "http://localhost:8080/" it correctly renders the file templates/index.html, which means that PHP and Fatfree are working. But If I go to "http://localhost:8080/1" then it doesn't work. I get the following error:
Not Found
The requested URL /1 was not found on this server.
If I change the first part to
F3::route('GET /anotherthing','home');
function home() {
echo F3::render('templates/index.html');
}
then "http://localhost:8080/anotherthing" doesn't work either. It just works on the root. Any help?
MORE INFO
This is configured in httpd.conf
DocumentRoot "C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs"
<Directory "C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs">
Options -Indexes FollowSymLinks Includes
AllowOverride All
Order allow,deny
Allow from All
</Directory>
Modrewrite is enabled:
LoadModule rewrite_module modules/mod_rewrite.so
And .htaccess looks like:
RewriteEngine On
RewriteBase /fatfree/
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /fatfree/index.php [L,QSA]
"/fatfree/" base is due to an answer in another SO question that had a similar issue.
I also tried with the following .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L,QSA]
You really need to look at your server logs. If you haven't enabled them, I would suggest you do so. Apache has really good logs. If you see log entries and arent' getting a 500 server error, then it's usually not mod_rewrite.
If you want to be sure the rewrite module is loaded (on Linux), try this:
[root#server ~]# httpd -M 2>&1|grep rewrite
It will return something to this effect:
rewrite_module (shared)
Enable rewrite engine and route requests to framework
Try this, this fix works for me. Modified your .htaccess file just like the code below.
RewriteEngine On
# Some servers require you to specify the `RewriteBase` directive
# In such cases, it should be the path (relative to the document root)
# containing this .htaccess file
#
#RewriteBase /
RewriteRule ^(lib|tmp)\/|\.(ini|php)$ - [R=404]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [END,QSA]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
You can try this .htaccess:
RewriteEngine On
RewriteRule ^(app|tmp)\/|\.ini$ - [R=404]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L,QSA]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
There is config.ini file in root of fatfree
[globals]
DEBUG=3
UI=ui/
DEBUG=3 will let f3 show all errors from internal, and changing ui value is important since you need to keep your templates inside ui folder, so in this configuration you've to change it to your own template directory.
As per f3 documentation, static calling convention (F3::xxx) has been deprecated, better option is call an instance of F3 (like $f3->xxx).
Also pleas check HTTPD error logs for isuues related mod_rewrite and .htaccess.
--
You only need that in your index.php
$f3=require('lib/base.php');
$f3->config('config/config.ini');
$f3->config('config/routes.ini');
$f3->run();
And then in your config.ini :
[globals]
DEBUG=3
UI=views/
routes.ini :
[routes]
GET /=ControllerName->methodThatRenderTheView
GET /#pagenum=ControllerName->methodThatRenderTheView
The basic controller to render your view :
class ControllerName {
public function methodThatRenderTheView() {
$this->f3->set('view', 'template.htm');
}
}
I used to have the same problem as you.
My DocumentRoot in httpd.conf entry look like:
<Directory />
AllowOverride none
Require all denied
</Directory>
Using these .htaccess directives:
RewriteEngine On
RewriteBase /fatfree/
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /fatfree/index.php [L,QSA]
Couple with mod_rewrite are working properly on my end. I have the most recent f3 and wamp as of today.

htaccess redirects unexpectedly to folder

I have strange for me problem. I'm developing Zend Framework application and have this file structure:
/
/application/
/public/
/public/.htaccess
/public/index.php
/public/js/
/public/style/
/public/profile/
/.htaccess
My domain point to folder /
When I enter the address example.com/profile/ it goes to the controller profile and this is good.
When I enter the address example.com/profile the server redirects me to:
example.com/public/profile/
I would like to have a solution that whenever I request:
example.com/profile/ or
example.com/profile
The same page will be rendered but second version gives me a redirect and I don't know why.
The /.htaccess file is:
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
The role of this file is to route all traffic from / to /public but without any redirects.
The /public/.htaccess file is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Can anybody help me with this? Thanks.
I fixed this. Here is the solution if somebody have the same problem:
To fix this you have to set the following options and disable DirectorySlash
Options -Indexes FollowSymLinks
DirectorySlash Off
Now Apache shouldn't add trailing slashes at the end of uri when you pointing to directory.
This also disable redirect to the uri with trailing slash.
Optionally match the last /. Change your .htaccess file to this:
RewriteEngine On
RewriteRule ^(.*)/?$ public/$1 [L]
This works for me:
RewriteEngine On
RewriteRule ^/$|^(.*)$ /public/$1 [QSA,NC,L]

Categories