Why is my .htaccess code is not working? - php

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.

Related

Mod re write breaking url

Update I have re- done the code and folder system of my mod re write on localhost still nothing
the link only shows like this
http://localhost/cms/member/profile.php?member=10308
my entire .htaccess file
RewriteEngine On
RewriteRule ^cms/member/([^/]*)$ /cms/member/profile.php?member=$1 [L]
when i click on the link it does nothing. if i change in the browser to
http://localhost/cms/member/10308
it says page cannot be found.
Update the problem was this peice of code causing it to not display right
<?php
if(empty($_GET['member']) || $_GET['member'] <1000 ) {
redirect(ROOT_URI);
exit;
}
?>
But i need this code.
the url will work but i need to type it in manually it wont automatically change the url
As you are using an .htaccess file which is placed under the directory "cms", you should use the following directives:
RewriteEngine On
RewriteRule ^member/([^/][0-9]*)$ member/profile.php?member=$1
This will send the request http://localhost/cms/member/(any-number) to http://localhost/cms/member/profile.php?member=(any-number)
Note:
mod_rewrite should be enabled.
In httpd.conf (or apache2.conf), replace AllowOverride None by AllowOverride All to enable .htaccess
Restart Apache server.

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

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

GET, URL-Rewrite

Continued from my previous thread: GET, if, elseif
I'm using a CMS but it has URL rewrite somewhere through the files.
How would I go around getting the $_GET to work without the .php extension?
Options +MultiViews
That still won't let me visit /something.php it says not found. (Would it be because the pages aren't in /, they are in another directory using a template system?
If you are using apache HTTP server, then you should consider to use multiview option.
No need to use RewriteRule there.
In the htaccess file, add :
Options +MultiViews

Apache Url Rewriting won't work

In the httpd.conf file I have AllowOverride FileInfo. In the .htaccess file in top level of my webserver with all the other files, I have this:
RewriteEngine On
RewriteRule ^downloads/?$ index.php?page=downloads [L,NC]
But it doesn't work. mywebsite/downloads and mywebsite/downloads/ always give a 404 not found. Any idea why? Thanks. (mywebsite/index.php?page=downloads does work).
And I'm restarting apache every time I change it.
And when I put the code above in httpd.conf, the website won't even load at all, just blank, spinning safari wheel forever.
Its fine if I just do RewriteEngine On, but if I do anything else (RewriteBase, RewriteRule), the web browser spend ages trying to load and finally giving this error:
Safari can’t open the page “http://mk12.gotdns.com/” because the server where this page is located isn’t responding.
Anyone have any idea what's wrong?
EDIT: I'm able to make, for example, css files forbidden with rewrite, and it works, but any rule that goes from downloads to index.php?page=downloads makes the server not respond (see above), it doesn't matter what page, the website won't load at all. Any ideas..?
The best way to debug rewrite rules is to enable rewrite logging so you can see what's going wrong.
It Worked!! I was putting the code in the wrong spot. It was in httpd.conf, but at the end. Move it into <Directory> and its good. Thanks for your help!
EDIT: Also, I found that It won't work if the flags are like this: [L, NC]. It has to be [L,NC] (no spaces).
So the two problems where that it wasn't inside <Directory "/Library/WebServer/Documents">, and there were spaces between the flags. Hopefully this will help someone else in the future.
Have you tired adding a slash in front of the "download" like below
RewriteRule ^/downloads/?$ index.php?page=downloads
EDIT: Try the code below:
RewriteEngine On
RewriteBase /
RewriteRule ^downloads/?$ /index.php?page=downloads
I would try removing the trailing slash and question mark after downloads, and the leading slash before index.php.
RewriteEngine On
RewriteBase /
RewriteRule ^downloads$ index.php?page=downloads.

Categories