Apache url rewrite problem - php

requests -----> should be written to new url
/institute/dps -----> /institute.php?slug=dps
/institute/abc -----> /institute.php?slug=abc
/institute/123 -----> /institute.php?slug=123
I am using following rule in .htaccess
RewriteRule ^institute/(.*)$ /institute.php?slug=$1
However, it's not working. the page insitute.php get's execution, but the query string always comes empty.
Any suggestions?

That should work... If you try
RewriteRule ^institute/(.*)$ /institute.php?slug=$1 [R]
it should redirect formally, and you'll see the new URI
If you don't have the [R] in there it will issue the correct request, but you won't see ?slug= in the query string, but $_REQUEST['slug'] will be set.

Have you turned on Rewriting in your .htaccess file? (before your RewriteRule)
RewriteEngine On
If so - does your apache configuration allow you to use .htaccess file? (look for the line)
AllowOverride None
inside you httpd.conf
it SHOULD be
AllowOverride All
Finally make sure the url rewriting module is turned on in httpd.conf (it might be commented out)
LoadModule rewrite_module modules/mod_rewrite.so

Related

.htaccess url rewrite not passing parameter - could there be apache settings somewhere that is preventing this?

Okay, so this problem has completely stumped me and the other devs I work with. Here is the rundown:
I have a local dev environment setup with Mac Apache2 pointed at /Users/myusername/Sites/
Within /Sites I have two folders, /site-1 and /site-2, both of which have virtual hosts pointed at them site-1.dev & site-2.dev. Both site-1 and site-2 are running local installs of PerchCMS.
Within /site-2 I have an .htaccess file which I am trying to set up a URL rewrite that takes the URL /detail/slug-here and translates it into /detail.php?s=slug-here
I have tried the following rewrites (at the suggestion of PerchCMS support) and both have failed to pass the s param:
RewriteRule ^detail/([a-zA-Z0-9-/]+)$ detail.php?s=$1 [L]
RewriteRule ^site-2/detail/([a-zA-Z0-9-/]+)$ /site-2/detail.php?s=$1 [L]
Additional info:
Yes mod_rewrite is enabled in apache... in the same .htaccess file it totally works if I do a simple rewrite like this...
RewriteRule dangerzone.html index.php
One odd behavior that I've noticed is that if I remove everything from .htaccess I can still pull up detail.php by pointing my browser at /detail/test-item-1...(yes I have restarted my server) so its behaving as if there is still some sort of rewrite in place and loading detail.php sans param just as it continues to do with the rewrite in place - is this a clue that there is something off somewhere else in my server config? Note, RewriteRule dangerzone.html index.php does NOT work once it is removed from .htaccess.
Have this code in your site root .htaccess (inside /site-2/):
Options -MultiViews
RewriteEngine On
RewriteRule ^detail/([a-zA-Z0-9/-]+)/?$ detail.php?s=$1 [L,QSA,NC]
Important is to turn off MultiViews options here. 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.

htaccess not working on windows server 2003 with apache installed

I have Apache installed.
I have LoadModule rewrite_module modules/mod_rewrite.so un-commented and set every instance of AllowOverride None to AllowOverride All in http.conf.
My site is in a subfolder, so its like http://123.34.56.123/Website/ to get to it.
.htaccess file
RewriteEngine On
RewriteBase /Website/
Options +FollowSymLinks
RewriteRule ^index.php http://www.google.com/? [R=301,L]
But when I visit the above address, it does not redirect to google.com and, instead, shows the contents of index.php.
Update:
Added junk to the .htaccess file and http.conf to force a internal server error, and it still just goes to my index.php.
Did a var dump on $_SERVER, and found this out: ["SERVER_SOFTWARE"]=> string(17) "Microsoft-IIS/6.0", but can't find any program files around IIS.
Based on your question, some probable causes of your problem:
Incorrectly named .htaccess file. You call it an "htaccess file" repeatedly in your post. The filename must be .htaccess (with the leading .).
File in the wrong directory. Make sure this file is in the root directory of your site.
The module you need to enable isn't mod_userdir.so, but mod_rewrite.so. Look for a line like this one: LoadModule rewrite_module modules/mod_rewrite.so
Make sure the [R=301,L] is on the same line as the RewriteRule to which it applies, or you will have errors.
Also, don't do a 301 on this, even for testing -- it will permanently redirect traffic to your index.php to Google. That's what a 301 does. For testing, use a 302.
Converting comment to an answer, if your update to httpd.conf mysteriously fail to change anything on Windows, edit httpd.conf as Administrator (not a member of the administrator group, Administrator) to avoid Windows silently doing "data redirection" of httpd.conf edited out of Program Files.
http://blogs.windows.com/windows/archive/b/developers/archive/2009/08/04/user-account-control-data-redirection.aspx

CakePHP missing controller on EC2

I have a Cake 2.3.8 app that works fine on localhost and I uploaded it to an Ubuntu 12.04 EC2 instance, but on the index/home file I'm getting a "Missing Controller" error.
The CSS, header and footer load fine on the home/index page, but it can't seem to find the controller. Every single other page/controller of my app won't even load, I get a 404 error.
I tried this but my issue still persists.
What else should I look for that would cause it to not find the controller and not even load any other pages?
Update
I moved my contents to /var/www so it would be accessed through my-ec2-instance.com. When I go to my-ec2-instance.com/index.php the page displays (which is PagesController/home.ctp). However, anything else gives a 404 error. Modrewrite is also enabled
I can also access my actions when I reference index.php, although it displays in mobile format for whatever reason (I have a responsive template). Ex: www.my-ec2-instance.com/index.php/users/login works, although it's in a mobile format
www.my-ec2-instance.com/index.php //works
www.my-ec2-instance.com/index.php/users/login //works, but mobile format
www.my-ec2-instance.com/users/login //404 error
Please connect your instance via putty then press
sudo nano /etc/httpd/conf/httpd.conf
Check the given below line and remove the # tag
LoadModule rewrite_module modules/mod_rewrite.so
After that you can replace:
AllowOverride None
with
AllowOverride All
Note: AllowOverride None -> AllowOverride All.
From Crtl+X then press Y then press Enter (you can use this command according to your OS)
After that you can execute the command in putty
sudo service httpd restart
Then check your .htaccess file (please make sure your .htaccess should be correct) for cakePHP .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Sounds like an issue with your .htaccess file which is what would properly rewrite short URLs & handling controller routing.

Configure rewriting url in localhost with EasyPHP

I'm trying to configurate .htaccess to set rewriting URL in localhost, with EasyPHP, but all I get is 404 error.
My local website is C:\Projects\Test\, and in EasyPHP I created a new alias with "Test" name poiting to this location. So, to access this website, I type 127.0.0.1:8080/test in the browser.
The .htaccess file is addressed in the root of this website (C:\Projects\Test\ .htaccess). It's just an example, where everything typed redirects the user to "/user/inicial.php".
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-z,0-9,A-Z,_-]+)$ /user/inicial.php
</IfModule>
I've also follow these steps to configure apache to be able to run rewriting URLs:
Changed httpd.conf file (addressed in C:\Programs\EasyPHP-12.1\conf_files) where:
Uncomment "LoadModule rewrite_module modules/mod_rewrite.so" line;
Change all "AllowOverride None" sentences to "AllowOverride All".
It's everything that I have done. If someone has more options to give me, I'll really appreciate.

php URL rewrite wamp

I want to implement URL rewrite in wamp.
My ugly link is:
http://mysite.com/news.php?id=4654654&title=asdasdasdas-das-dasd-as-da-sda-d-asd-ads-as-da-sd
I want pretty URL to be
http://mysite.com/4654654/asdasdasdas-das-dasd-as-da-sda-d-asd-ads-as-da-sd
Using online generator on www.generateit.net/mod-rewrite/ for my link I get this .htaccess result:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /news.php?id=$1&title=$2 [L]
in wamp apache rewrite_module is checked, in httpd.conf I change AllowOverride None to AllowOverride All. I removed # sign from LoadModule rewrite_module modules/mod_rewrite.so
But when I click on hred with link like this http://mysite.com/4654654/asdasdasdas-das-dasd-as-da-sda-d-asd-ads-as-da-sd , I receive Not Found error The requested URL /myfolder/4654654/asdasdasdas-das-dasd-as-da-sda-d-asd-ads-as-da-sd was not found on this server
site root folder is in c:/www/myfolder
Can someone tell me what should I do to work this?
Thanks
EDIT
should I made some changes in my php file except href values?
create a new .htaccess file in your web root directory i.e c:/www/myfolder and place the code in it. and it should work.
The RewriteRule-part should start in a new line under RewriteEngine on.
There is a good beginner's guide with explanations at:
http://www.workingwith.me.uk/articles/scripting/mod_rewrite

Categories