Apache mod_rewrite not working - .htaccess

I have a problem with the mod_rewrite module for Apache 2.2
My code will neither work on localhost (Wamp on Win8 Pro, IPv6) nor on the webhotel (site5.com).
My goal is to generate SEO friendly URLs as:
www.xy.com/featured-artists.html
instead of:
www.xy.com/index.php?pageID=Artists
The PHP variable $pageID is defined on line 1 in my index.php:
<?php isset($_GET['pageID']) ? $pageID = $_GET['pageID'] : $pageID = 'Forside';?>
The code for the rewrite in my .htaccess file looks as follows:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([^/.]+)/?$ /index.php?page=$1
</IfModule>
Unfortunately that doesn't have any effect on the site. The .htaccess file is working properly though, since other sections like
ErrorDocument 404 /index.php?pageID=404
work as expected.
No matter what I do - it won't work. So I really hope that some of you can help me with that.
Thanks!

Mod_rewrite doesn't seem to be turned on. Your rules are inclosed in a IfModule container:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([^/.]+)/?$ /index.php?page=$1
</IfModule>
So the rules aren't going to get executed. You need to turn on mod rewrite in server config (or your webhost) if you want any of your rules to work. Otherwise, it doesn't matter what you try.
Once you had mod_rewrite loaded, your rules should do close to what you want. It'll take /something and internally rewrite it to /index.php?page=somethin.

Related

Using .htaccess is giving 500 error but working for file

I am not an expert using .htaccess but here is my scenario:
My .htaccess is in the root.
The only content is below.
To access the pdf file is working fine but not when I am trying to access the file with parameters that is going to return data or send data.
I know I am missing something but what?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^admin$ login.php
RewriteRule ^admin-system/balance/get$ source/Support/Action.php?action=balance-get
RewriteRule ^admin-system/balance/list$ source/Support/Action.php?action=balance-list
RewriteRule ^admin-system/balance/set$ source/Support/Action.php?action=balance-set
# This one is working fine.
RewriteRule ^admin-system/terms$ app/helper/files/term.pdf
Options +Indexes
</IfModule>
HTTP 500 - when talking about htaccess - is a syntax error. IMHO, just remove the following line:
Options +Indexes
It's highly possible, that Options is not allowed there - the apache error log will give you a clue about it.

htacces rewrite single file

I want to rewrite a specific file on my website to another one by using htaccess:
# General
RewriteEngine On
RewriteBase /
Options All -Indexes
Options +FollowSymLinks
# Rewrite file
RewriteRule ^/file.html$ /dir/file.html [L]
This is the .htaccess code i'm using based on snippets i found on the internet.
Somehow this is not working, the server is returning a 404-Not-found error.
I can't see any difference with example's that are said to work, like in
Rewriting path for a specific file using htaccess
Edit:
If I place file.html in the root-folder, I can view it. So the rewrite definitely is not happening.
RewriteRule does not receive leading slash. Write so:
RewriteRule ^file.html$ /dir/file.html [L]

htaccess simple redirect doesnt work

Hi I need to redirect using htaccess every request which points at:
http://www.mydomain.com/index.php?option=com_content&view=category&layout=blog&id=293&Itemid=387
to this url:
http://www.otherdomain.com
I've try to do it by:
redirect /index.php?option=com_content&view=category&layout=blog&id=293&Itemid=387 http://www.otherdomain.com
But it doesnt work. So I need Your help.
Better to use mod_rewrite for this stuff.
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 /
RewriteCond %{QUERY_STRING} ^option=com_content&view=category&layout=blog&id=293&Itemid=387$
RewriteRule ^index\.php$ /? [L,R=302,NC]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
I think someone may need to extend my answer but you'll be looking to do something along the lines of:-
RewriteRule ^([^/]+)/? index.php?option=$1 [R=301,L]
The rule will require a regular expression so the server can compare the request.

.htaccess mod-rewrite -- Not Redirecting

Okay, so I have a script that works like adf.ly; you submit a URL, the Url is shortened and then an interstitial advertisement shown before you're taken to your URL. I have the following .htaccess located in the root:
DirectoryIndex index.php
FileETag none
ServerSignature Off
Options All -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([0-9a-zA-Z]{1,6})$ fly/?to=$1 [L]
RewriteRule ^([0-9]{1,9})/banner/(.*)$ fly/?uid=$1&adt=2&url=$2 [L]
RewriteRule ^([0-9]{1,9})/(.*)$ fly/?uid=$1&adt=1&url=$2 [L]
</IfModule>
The script is generating the shortURL's (you can try here: http://www.twitsym.com/short/) however it is not redirecting to fly.php and then the final URL. I'm terrible with .htaccess and have little to no knowledge. Can anybody edge me further as to what might be causing the problem?
Directory structure is:
.../
.../fly/index.php
Thank you again, StackOverflow!
it seems that you will need the LogLevel directive somewhere in your httpd.conf or vhost.conf if this is a virtual server. See this post for a similar question.
The code would be:
RewriteLogLevel 3
RewriteLog "/usr/local/var/apache/logs/rewrite.log"
Where is the htaccess supposed to be redirecting to (i.e. which URL should lead whereto?)
Additionally, you asked for being redirected to fly.php, however it does not occur in your .htaccess. Was this your intention?

htaccess rewrite rule similar to WordPress

Ok I have a script in the folder 'intake'. The index.php file auto creates a list of urls. Those urls are in the format:
/intake/4/Westrop
I have an .htaccess file in the intake folder. I want it to redirect the url to a FILE.
The above example would then become /intake/redirect.php?id=4&name=Westrop
Here's what I have so far:
DirectoryIndex index.php
Options All -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)/(.*)$ /intake/redirect.php?id=$1&name=$2 [L]
</IfModule>
But for some reason if I type /intake or /intake/index.php I get a 404 error. According to firebug its trying to take "intake" and turn it into "intake.php"
Any ideas?
Place the following code in .htaccess file in website root folder:
RewriteEngine On
RewriteRule ^intake/(\d+)/([a-z0-9\-_]+)$ /intake/redirect.php?id=$1&name=$2 [NC,QSA,L]
P.S.
Do not use this sort of pattern -- ^(.*)/(.*)$ -- in your case for longer URLs it will work not as you would expect. It will match your URL .. but in a wrong way.
Start your .htaccess with this option:
Options +All -MultiViews -Indexes

Resources