Wrong url redirects to index page - php

I type xyz.com/1234(1234 is jargon) it takes me to xyz.com/index.php. I am using apache and mod_rewrite does not have any rule for this .Index page is not even the default page setup by the DirectoryIndex.I am puzzled why it takesto the index page.

Try whether it gets better with
Options -Multiviews
in the .htaccess file. If it doesn't, there must be a rewrite rule somewhere, or maybe an ErrorDocument directive (although that shouldn't redirect).

You can see if it's mod_rewrite doing it to you easily by cranking up rewrite logging to insane levels:
RewriteEngine On
RewriteLogLevel 10
RewriteLog "/var/log/apache2/rewrite.log"
Post your apache config and the relevant .htaccess file and we might be able to be more help.

Related

Strange issue using Mod_rewrite and $_GET variable

After reading tons of SO questions, asking friends and so one, I'm coming here with a strange issue regarding Apache mod_rewrite.
I'm trying to catch http://api.server.com/.../results.php?id=X URL though a RewriteRule.
Quite simple you'll say, I know it, my .htaccess file content is :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^results/(.*)$ results.php?id=$1
results.php is quite simple for debugging reasons, looks like
var_dump($_GET);
But this script always return array 0 { }
Shall I specify that I've already tried to clear the flags, and change the (.*) class by others, without effects.
Thanks for your help.
You will need to disable MultiViews option here:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteRule ^results/(.*)$ results.php?id=$1 [L,QSA,NC]
Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
Your rewrite rule does not match the URL you are using (http://api.server.com/customer/2/results.php).
The correct URL according to your rule and setup is:
http://api.server.com/customer/2/results/123
However, you mention having placed everything in the /2/ folder. If 2 is the ID you are trying to get, it cannot work -- URL rewriting only works with non-existing paths.
Instead you should place your .htacess and results.php file in the customer folder, and use the following URL:
http://api.server.com/customer/results/2
Or change your rule and URL to:
RewriteRule ^([0-9]+)/results$ results.php?id=$1
http://api.server.com/customer/2/results

Why is my .htaccess code is not working?

My htaccess file code is not working even it is right i found on many website for this each website has same code here it is :-
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^products/([a-zA-Z]+)/([0-9]+)/$ index.php?action=$1&sub_cat=$2
now this thing is not working www.example.com/products/something/3/
anything else i am forgetting please help me.
Because you have a products.php file and your URL looks like www.example.com/products/something/3/, a module called "mod_negotiation" is processing the request before mod_rewrite can. The Multiviews option will allow mod_negotiation to try to "guess" what a request is for, and it sees /products/ in the URL and the file /products.php and assumes that's what the request is for, serves the request via the products.php script and mod_rewrite never gets a chance to do anything.
Solution?
Turn off multiviews:
Options -Multiviews
by adding that option anywhere in your htaccess file.

How Mod Rewrite interacts with logs

I'm using CakePHP, and I'm not understanding how mod_rewrite affects custom logs.
In .htaccess:
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L,NE]
(index.php is where CakePHP starts to route the request)
And then in /etc/apache2/apache2.conf:
LogFormat "%U %q" custom
CustomLog "/var/log/apache/custom.log" custom
But for a request /foo?bar=1, I see
/foo ?url=foo&bar=1
I don't understand what is happening.
It seems like the log happens before rewrite with %U, but AFTER rewrite with %q. Is this expected behavior? Can I change it? (There are severe limits on my log size, and logging every path twice is undesirable.)
I'm not sure what Apache is doing here, but I can get CakePHP to work without using the query string at all:
RewriteRule ^(.*)$ index.php [QSA,L,NE,E=URL:$1]
And then in index.php:
$_GET['url'] = substr(getenv('REDIRECT_URL'), 1);
This seems a lot less hacky anyway than the normal CakePHP way.
This is not the way to go about it to look what mod_rewrite is doing; you need to see whats going on in the rewrite process. You need to enable rewrite log in order to accurately see what's going on in there:
The code is:
RewriteLog "/usr/local/var/apache/logs/rewrite.log"
You'll need to then restart apache considering you have the rewrite module enabled. This is going to help you in debugging. In addition, you have to put this in the virtual host entry.

htaccess rewrite rule not passing url vars when script file is same as rewrite directory

I'm trying to create nice urls with .htaccess files and have come across a weird issue.
I want to change portfolio.php?id=2 to /portfolio/2/
seems pretty simple solution
RewriteRule ^portfolio/([0-9]+)/$ /portfolio.php?id=$1 [L]
this does redirect to the correct script but when i try and run <?=$_GET['id'];?> it is undefined. but if change the script to something that does not equal the fake directory it works.
RewriteRule ^portfolio/([0-9]+)/$ /portfolioitem.php?id=$1 [L]
and just to make sure that it wasn't being caught by any other rules I tested this
RewriteRule ^portfolioitem/([0-9]+)/$ /portfolioitem.php?id=$1 [L]
and again it failed to pick up the id paramater!
any ideas?!
Cheers
This sounds suspiciously like a Multiviews related problem coupled with some PATH_INFO. The Multiviews option is part of mod_negotiation and it will try to match a requested URL path to a file path. It sees:
/portfolio/2/
And sees that there's a /portfolio.php file in the filesystem and assumes that this is what you want (which it is, but not in the same way). I'm willing to bet that instead of looking at $_GET['id'], which is blank of course since there are no GET params, if you look at $_SERVER['PATH_INFO'], you'll see it set to /2/. This is equivalent to going to:
/portfolio.php/2/
where the /2/ part gets passed to portfolio.php as part of the PATH_INFO. And since mod_negotiation is further up in the processing pipeline than mod_rewrite, your rewrite rules never get applied.
Try turning off multiviews. You can do this in your htaccess file using the Options directive (assuming your host has allowed Options):
Options -Multiviews

.htaccess for hiding url details

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^WR-(.*)\.html$ WR.php?act=show
i have created .htaccess file to rewrite WR.php?act=show to .html extension and save this file in a folder where my source file are residing. but it is not working can anybody help me please....
try this htaccess rule generator
or this,
these will definitely help you a lot.
The rule looks fine to me. Copying and pasting into a .htaccess on my own server redirects WR-foo.html to WR.php?act=show as expected.
Depending on what exactly is happening (are you seeing an error? is it just not redirecting? redirecting to the wrong place? etc), you might want to try enabling logging for mod_rewrite. See the RewriteLog directive (and RewriteLogLevel just below that). The Apache error log is also always worth a look.

Categories