rewrite index.php?p=page to page - .htaccess

My urls currently look like this: http://www.morrisononline.com/index.php?p=contact
I would like to rewrite them to http://www.morrisononline.com/contact
The content for (in this case) contact comes from the directory /pages/ (www.morrisononline.com/pages/contact.php)
I have tried the following in the .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z0-9-_]+)/?$ index.php?p=$1 [NC,L]
I have also tried other combinations too (I'm not very good at this and trying lots of suggestions from other forum posts etc) but nothing has any effect. No error messages and no rewrites either. Did I do it wrong or could there be another command before that, that overrides this?
Do I have to refresh some cache or restart the server for this to work?

Make sure you have AllowOverride All or at least AllowOverride FileInfo in your site's config for the site's document root. And make sure the htaccess file is in the document root, is readable by everyone. If it's still not working, make sure that you've got AccessFileName set to .htaccess.

Related

Set an index page for a specific folder in .htaccess

What I want to do should be quite simple: when the admin writes www.example.com/admin I want that he's addressed to www.example.com/admin/admin_index.php.
I wrote this rule and I checked on other posts here on Stackoverflow: it apparently seems to be correct, but it actually doesn't work.
RewriteEngine On
RewriteRule ^/admin/?$ /admin/admin_index.php [L,NC]
Has anyone any clue on why the redirect doesn't work, since it "stays" at www.example.com/admin outputting (obviously) the 403 error?
There is no need to use a rewrite rule. Just use DirectoryIndex directive in admin/.htaccess:
DirectoryIndex admin_index.php
This will load admin/admin_index.php when a request comes for http://domain.com/admin/

.htaccess RewriteRule returns a blank page

I'm trying to solve this for more than two hours now.
I have a personal site which uses .htaccess to manage urls.
It looks like this:
RewriteEngine on
RewriteBase /
...
RewriteRule ^sklad/?$ index.php?action=sklad
RewriteRule ^sklad/user/([0-9]+)?$ index.php?action=sklad&user=$1
RewriteRule ^sklad/folder/(.+)?$ index.php?action=sklad&folder=$1
RewriteRule ^sklad/file/(.+)?$ engine/ajax/sklad.php?file=$1
RewriteRule ^sklad/logout/?$ index.php?action=sklad&op=logout
...
RewriteRule ^admin/?$ admin.php
RewriteRule ^admin/news/?$ admin.php?action=news
the first five ones work fine. The admin/ one works fine. But when I try to access admin/news/, I get a blank page. No errors displayed or logged by Apache, and no output. admin.php?action=news is working fine.
Both sklad/ and admin/ folders physically exist on the server. BUT when I rename the admin/ folder to something else OR change the the last RewriteRule to something like
RewriteRule ^admin123/news/?$ admin.php?action=news
I can access admin123/news/. If it has something to do with the actual folder existing on the server, then why the first five rules are working? This doesn't make sense.
I'm out of ideas, hope someone here helps...
Yes, it's called news.php... I renamed the file and exerything is fine now, thanks! Didn't know about this, pretty unobvious bug (or not?)
It's not a bug, it sounds like content negotiation (via mod_negotiation) is turned on and it's doing something you don't want. Negotiation can be turned on via a type map or the MultiViews option. Typemaps are a little explicit to setup, so I'm assuming since you don't know why this is happening, you haven't set of a specific type that maps to news.php. So you've probably got Multiviews turned on. You can turn it off by either removing it from an Options statement:
# remove this word -----------v
Options Indexes FollowSymLinks Multiviews
This could be anywhere, in your htaccess, server config, vhost config, some config include file, etc. So you can also explicitly unset it in your htaccess file (as long as you aren't also explicitly setting it in the same file):
Options -Multiviews
I am not to good with htaccess and RegExp but i think admin/news will fall into your first htaccess rule.
RewriteRule ^admin/?$ admin.php
It wont proceed on your second rule on admin which is:
RewriteRule ^admin/news/?$ admin.php?action=news
Its the same problem I was facing before.
Try to modify your admin.php, echo/print something when there is no params pass. Something like this:
if($_GET['action'])
{
echo 'With Parameters';
}
else
{
echo 'No Parameters pass';
}
Debugged it that way.

URL Rewriting not working?

im new here.
Im trying to rewrite some urls on my site only for some reason no matter what I try I cannot get them to work!
My directory on my server has the following...
index.php
user-profile.php
.htaccess
On my index.php there is a number of users that all have a view more details button that links through to user-profiles and posts an ID using GET method as such...
http://mysite.com/user-profile.php?userID=2&firstName=Martin&lastName=FAM
However Id like to format them as so...
http://mysite.com/people/2/Martin/FAM
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /user-profile.php?userID=$1&firstName=$2&lastName=$3 [L]
Can anybody give me some reason as too why this isnt working?
The regex should be
^people/([^/]*)/([^/]*)/([^/]*)$
You need to make sure you have this option set in your document root directory in Apache's httpd.conf file:
AllowOverride All

htaccess rewrite working offline on WAMP but not online on linux host

I'm just going to explain my problem here :
http://mysite.com/movie/20000
should be rewritten to
http://mysite.com/movie.php?id=20000
my htaccess file :
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^movie/([0-9]+)$ movie.php?id=$1
On my localhost WAMP installation this works fine, but when I put it online on my linux host it doesn't completely work. It does go to the movie.php page, but it seems it gives no GET parameter id.
Edit :
if I use
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^movie([0-9]+)$ movie.php?id=$1
then
http://mysite.com/movie20000
Goes to the correct page, but I would like /movie/20000 and not just /movie20000
It also seems that my host automatically rewrites a visit to mysite.com/movie to mysite.com/movie.php
After searching for a long time, and pulling some of my lovely hair out I found the solution.
I just added
Options -MultiViews
to my htaccess file and that fixed it.
Why? Because .php was being added to urls without an extension, which I really did not need.
This should work.
RewriteRule ^movie/([0-9]+)$ http://mysite.com/movie.php?id=$1 [NC,L]
Don't forget the [NC, L] it means Case insensitive, last rule... If you don't, it will continue to process through your htaccess rules, maybe triggering other ones.
Still, the stuff below is good advice.... :)
Check to see if the Rewrite module is loading with apache. Look for this line in the httpd.conf file:
LoadModule rewrite_module modules/mod_rewrite.so
Check to see if your apache config allows for .htaccess files for your system or in the virtual host definition.
Also, test with a simpler rewrite catch all and test that alone to see if it's working at all like this (get rid of everything else in your htaccess file to limit the test):
RewriteEngine On
RewriteRule ^(.*)$ http://www.google.com [L,R=301]
Any request to your site should go to google if the configuration for apache is correctly set.

Apache mod_rewrite works in .htaccess but not httpd.conf

I'm experimenting with mod_rewrite for the first time (I'm a web newbie, but trying to learn). I'm trying to get bob.html to redirect to alice.html (read: URL stays the same, page content is alice.html). Both files are in /var/www/. I'm running Ubuntu 10.10 w/Apache 2.2.16.
Here's what works in the .htaccess file placed in the /var/www/ directory:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^bob.html$ alice.html
</IfModule>
This behaves as expected, but it does not work when moved to the httpd.conf file (just learning best practices w/performance and such). I read somewhere that when using mod_rewrite in httpd.conf file, leading slashes are required, so my httpd.conf file looks like:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/bob.html$ /alice.html
</IfModule>
I also read that /etc/apache2/sites-enabled/000-default needs to have AllowOverride set to All.
<Directory /var/www>
...
AllowOverride All
...
</Directory>
I think that's just for use with .htaccess but I wasn't sure so I just left it in.
With all of these configuration settings, the redirection does not work. I've tested to make sure the file itself is being read (someone suggested httpd.conf isn't used anymore and apache2.conf is used instead) by inserting erroneous code. I've taken a look at a couple other questions/answers but I still cant figure it out.
Edit: It should be noted that I'm using /etc/init.d/apache2 restart after each change to httpd.conf to restart Apache and (hopefully) reload the configuration.
Try wrapping the rewriterules in your httpd.conf file in <Location /></Location> tags. The seems to alter the behavior making it more similar to the .htaccess file.

Resources