Strange situation mod_rewrite and PHP - php

I do not understand something ...
I have CMS written by me and have some strange problem about hidden error from "mod_rewrite". In apache2 error.log "mod_rewrite" giving :
File does not exist: /home/path/to/request, referer: http://IP/request?view=1
my $_GET / $_POST request are all empty
$_SERVER['REQUEST_URI'] is ok (returns request?view=1)
$_SERVER['QUERY_STRING'] is also empty
also i install Drupal CMS to test is it problem on web server, but Drupal works just fine... then i copied drupal ".htaccess" file to my CMS in order to fix mod_rewrite preferences. (Drupal do not create apache error like my )
No luck, i have same problem again.
also tried with all options in .htaccess from Drupal CMS
I try to turn on RewriteLog (several attempts) but without result, not a single log file was not created.
Am i something missing or ... just my cms sucks
and one more thing, my CMS work just fine on other webserver with exactly the same files .... ?????
RewriteEngine On
Options -Indexes
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
#tested RewriteRule ^ index.php [L]
#tested RewriteRule ^ index.php [L,QSA]
#tested Options FollowSymLinks
#tested Options -MultiViews
#tested RewriteBase /
P.S. sorry for my bad English

Not sure if that's your whole .htaccess but you need to enable rewrite, here's a rewrite I use and its never failed:
RewriteEngine On
Options -Indexes
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
The RewriteRule passes everything (.*) to a route parameter in index.php then my router class handles the controller ect, the QSA flag will also allow me to pass extra GET/POST values to any part of the script.
hope it helps
ps I dont add the RewriteCond %{REQUEST_FILENAME} !-d because I dont allow folder views and I dont serve content from a folder, everything goes through a controller inc (images,css,js), so there is no need for this in most cases. And it also protects the folders like core & template ect ;)

finally I found the solution of my problem!
The answer is very,very,very simple...
In apache2 configuration (on Ubuntu) I found the <Directory tag is duplicated by default ... I just erase all directories tags and create one basic.
reload apache2 configuration and every thing starts working just fine

Related

Using apache mod_rewrite to redirect to php file

I'd like to redirect the following url:
http://www.example.com/food/?p=11&q=22&r=33
to
http://www.example.com/food/api.php/DBNAME/?p=11&q=22&r=33
As you may have noticed, DBNAME is the name of a database.
With the API I'm using it is necessary to include it in the URL.
Here's a brief folder structure of my program:
food/
->include/
->models/
->.htaccess
->api.php
->config.php
->test.php
I tried writing the rules on the .htaccess file but I get an Internal server error.
My .htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ api.php/DBNAME/$1 [L]
</IfModule>
I can get my head around achieving the redirection.
I also tried
RewriteRule ^food/(.*)$ api.php/DBNAME/$1 [L]
but this rule just lists the files in the directory food.
Any ideas?
Thanks!
PS: The query string is not just p=11&q=22&r=33 it may use any letter or word such as type, quantity and so on. p=11&q=22&r=33 is just an example.
Edit
From the link that #sanj provided I changed my .htaccess ffile to this
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/food/?$
RewriteRule ^(.*)$ api.php/DBNAME/$1
</IfModule>
It works partially. Why partially?
I have two servers, one for testing and one for production. It works well on the testing server but not on the production server. I get an Authorization Required error. I believe it's due to the configuration in my .htaccess because if I remove the mod_rewrite module and access the url directly it works. Any ideas?
Found my mistake. The folder was protected by Basic Authentication. I needed to include login parameters in my code. I can't believe I overlooked that.
I think this will work:
<IfModule mod_rewrite.c>
RewriteBase /food
RewriteEngine on
RewriteCond %{QUERY_STRING} /DBNAME/
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^$ index.php/DBNAME/?%1 [L]
</IfModule>

Changing the actual URL without changing display URL using htaccess [duplicate]

So, I've this problem:
Base Website located at http://example.com/
Second Website located at http://example.com/web2/
People making various requests to the second website like
http://example.com/myWeb/pg1 and http://example.com/web2/pg2
Recently and due to some other issues I need to have a custom new path for the second website but also keep the first one working.
The ideia is to allow users to access the second website over the two following addresses:
http://example.com/web2/
http://example.com/alternative-url-web2/
The folder /web2/ actually exists on the server, but how can I simulate the folder /alternative-url-web2/ and "redirect" the requests to /web2/?
Please note I don't want the URL on the browser to change, this must be a "silent redirect". And I also make sure that all other requests like http://example.com/other are not redirected by the second website.
Thank you.
Update:
According to #anubhava I could simply solve this issue by adding in my .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^alternative-url-web2(/.*|)$ /web2$1 [L,NC]
This is probably working fine but I noticed the following:
http://ex.com/alternative-url-web2 is redirected to http://ex.com/web2/ (changing browser URL);
http://ex.com/alternative-url-web2/ is redirected to http://ex.com/(changing browser URL);
http://ex.com/alternative-url-web2/someRequest works fine and does NOT change the browser URL;
http://ex.com/alternative-url-web2/index.php works fine and does NOT change the browser URL;
Site Note:
At /web2/ there's an .htaccess that might be cause the wired redirect behavior above... So here is the file contents:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</IfModule>
Can the internal RewriteRule to index.php be causing all this? If yes, how can I fix it?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^alternative-url-web2(/.*|)$ /web2$1 [L,NC]
Alternate code:
RewriteRule ^alternative-url-web2/?$ /web2/ [L,NC]
RewriteRule ^alternative-url-web2/(.+)$ /web2/$1 [L,NC]
This is a pretty simple rewrite. In the htaccess file in your document root, just add the following:
RewriteEngine On
RewriteRule ^alternative-url-web2/?(.*)$ /web2/$1 [L]
Unlike a redirect, which makes the browser/client send a new request for a new URL (thus changing what's in the browser's location bar), a rewrite happens entirely on the server's side.
By the way, in order to follow the trail of htaccess redirects, you could add something like this to each of them:
Header add X-Remark-Rewrite "/path.to/htaccess"
You can inspect these in the response in the developer tools.

unextected redirection un xampp for windows

i'm creating a website, before uploading the site to web hosting i'm using XAMPP on windows. i was trying to use Search Engine Friendly URLs, for example:
http://localhost/mysite/[something]
to
http://localhost/mysite/index.php?p=[something]
i tried this .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([0-9A-Za-z]+) index.php?id=$1 [QSA,L] #and also tried http://localhost/mysite/index.php?p=$1
before this i tried more examples but they didn't work. The real problem is that for example if y type
localhost/mysite/some_page
i get an error (403), using the [R] flag i realized that redirection is: http://localhost/C:/XAMPP/htdocs/mysite/index.php?p=some_page
i removed htaccess file and restarted apache but problam persist with some urls
Perhaps I'm trying to solve a different problem, but if the [something] always leads to content served by your index.php, then just send all requests to index.php and let that page use the URL string to get the variable you need to serve the right content (ie the string after the /).
You may need to turn off MultiViews in your .htaccess using Options -MultiViews.
The following is what you need in your .htaccess
RewriteBase /
# Where "/" is the location of your index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA, L]
Hope this helps.

Apache htaccess mod rewrite redirection using Controller GET variables in PHP index page

I am working with a custom MVC PHP framework and the index page (acting as a router) receives a GET variable "do" which contains the path that it will route to. If this variable is not set, it defaults to the Auth controller, method login.
require_once('config.php');
$controllerAction = isset($_GET['do'])?$_GET['do']:"auth/login";
require_once('core/main.php');
Then the index page (source code above) passes this $controllerAction to the main.php file, which autoloads the main controller and then loads the requested controller.
Thus, the URIs in this framework are of the form mysite.com/?do=controller/method/variable and I need it to be in the form mysite.com/controller/method/variable.
Here is the .htaccess file I tried to use, it just didn't work (I have other htaccess files working on the same server so it's not an Apache problem) :(
RewriteEngine On
RewriteRule ^([^/]*)$ /?do=$1 [L]
Someone suggested that I can do this using PHP but I am not sure how to go about that.
Edit:
The error is that I get "This page cannot be displayed", 404 errors, whenever I try to directly access the mysite.com/controller/method links rather than the default mysite.com?do=controller/method
Further Edit
(please note that other virtual hosts work fine on my localhost):
(XAMPP) Apache Virtual Hosting Info:
<VirtualHost *:80>
DocumentRoot "D:\sites\mysite.com\root\wwwroot"
ServerName mysite.com
ServerAlias mysite.com
<Directory "D:\sites\mysite.com\root\wwwroot">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
File structure (Windows):
D:\
--sites
----mysite.com
--------#client_details
--------root
-----------#devfiles
-----------#vars_pwd
-----------wwwroot
--------------config
--------------core
--------------application
------------------controllers
------------------libraries
------------------models
------------------views
----------------------css
----------------------javascript
----------------------images
----------------------icons
First of all, there are some issues with your .htaccess contents. It's always a good idea to not rewrite if a file with the requested name exists. This allows you to have an img/ folder for your images or any other static content like css files, javascript, downloads, etc.. The first RewriteCond tells Apache to only rewrite if no folder with this name exists. The second one does the same with files. Then you probably want the QSA (i.e. Query String Append) option, which will pass all other GET variables to your script.
Under this conditions you can simplify the regex and use this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?do=$1 [L,QSA]
You might be surprised because this is more or less the same as others posted. I use similar things for many of my projects and I've just tested it, I can guarantee that it works. There must be something wrong with your apache config.
When you have problems with mod_rewrite, the first thing you should try is to enable the module itself. Type these commands as root in your shell:
a2enmod rewrite
/etc/init.d/apache2 restart
The first one activates the module (or complains with Module rewrite already enabled if everything is ok) and the second one restarts your Apache server. The path may of course be different on your server.
Then you have to make sure that your VHost config allows you to use .htaccess files and do rewrites. This means AllowOverride must be set to at least FileInfo (or All). You could also try to put the rewrite rules right into the config file. Your config should look similar to this:
<VirtualHost *:*>
ServerName test.example.com
ServerAlias www.test.example.com
DocumentRoot /home/sites/test/
<Directory "/home/sites/test/">
Allow from all
AllowOverride All
Options +Indexes
</Directory>
</VirtualHost>
Note that you have to restart Apache if you change anything in there.
If that all doesn't help, it's always a good idea to have a look at the error logs. On my system they're located at /var/log/apache2/error.log (debian). They might give you more information on what's going wrong.
Try
RewriteEngine On
RewriteRule ^([^/]*)$ index.php?do=$1 [L]
Try
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?do=$1 [L]
Check your apache logs, access logs specifically. If the folder is present in the web root, then you should be able to access it directly :). You might also want to check if you have duplicate virtualhost entries for the same site by chance.
This one is my customized MVC framework which is based on cake
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?do=$1 [QSA,L]
</IfModule>
May be this should help. The typical URL pattern for this site.com/controller/method
I don't know what your domain setup is like, but here are some suggestions.
If your code resides in the root of your folder, and the index file is called index.php try the following:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?do=$1 [L,QSA]
If your website exists in a subfolder e.g. www.example.com/site/, and the index file is index.php Then try the following (change /site/ to whatever your folder is).
RewriteEngine On
RewriteBase /site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site/index.php?do=$1 [L,QSA]
If you still get the 404 error message then do the following:
Make sure your site allows .htaccess files to be processed by checking AllowOverride is set to all. If you don't have access to the necessary config files to check, a simple test is to setup an .htaccess rule to redirect to a dummy file on your system. If it works, then your .htaccess is being executed fine.
Have a look at your MVC framework to see what page it's actually sending the request to. The problem may be that you haven't defined a handler for that particular request, and the default action of your MVC framework is to throw a 404 error.
Edit: Just reading your description, I notice you said that the URL should basically be something like mysite.com/?do=controller/method/variable. If it has be very strict about this format, then you'll also need to put in rules for removing any leading or trailing slashes, e.g. the following re-write rule should do it:
RewriteRule ^\?(.*)\?$ /index.php?do=$1 [L,QSA]
(This makes the leading and trailing slashes optional, but it should remove them from the actual value you pass to do).

Placing .htaccess file in var/www folder messes my website up

I am playing with mod_rewrite now, and have successfully enabled it.
However, I need to put a htaccess file inside var/www/ in order to achieve what I want, which is to rename Urls simply...
When I place it my website becomes very strange and nothing basically works...
Is there any code I need to put into the htaccess file in order for things to act normally?
Here is the htaccess file I have so far:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/ad\.php
RewriteRule ^(.*)$ ad.php?ad_id=$1 [L]
My DocumentRoot is also set to var/www/ and my entire website root is there... (index.html etc etc)...
What am I missing about the htaccess?
If you need more input let me know...
I suspect that none of your css files, js or images are loaded. Furthermore, none of your links work either. If so, the problem could be in the RewriteRule cause basically that rule is telling apache to pass all requests to ad.php
You need to fine tune your RewriteRule, so that only the ad links are being affected by the rule.
First, by expanding the RewriteRule like this:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_URI} !^/ad\.php
RewriteRule ^(.*)$ ad.php?ad_id=$1 [L]
These 3 lines that I've added are telling apache to apply the rule only if the requested filename is not a directory, an existing file or a symbolic link - this should take care of the static content, such as the css and images. If your other pages where you're links are pointing at, are also physically on the HDD of the server (plain html or php files), should start working again.
But, as I already said on this question of yours (Little mod_rewrite problem) you need to fine tune that rule, so that only ads are being met by the rule and nothing else.
This isn't a complete answer but it will give your more information that can help you.
You can put options on the same line Options +FollowSymLinks -Indexes
Not all hosts allow .htaccess, or not all .htaccess commands. This can cause problems and pages not to work.
Try commenting out everything (using # sign in front of each line), then, starting at the top, uncomment until you find the problem line.

Categories