Basis mod_rewrite rule results in 500 internal server error - .htaccess

I have this very basic rewrite rule, no matter what I try, results in an Error 500.
RewriteEngine On
RewriteRule ^folder/(.*) /folder/index.php?Alias=$1 [L]
My httpd.conf file has the following content: (which seems OK to me)
<Directory "/var/www/html">
Options -Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
<IfModule mod_suphp.c>
suPHP_Engine On
suPHP_UserGroup webapps webapps
SetEnv PHP_INI_SCAN_DIR
</IfModule>
</Directory>
Any suggestions on what might be going wrong? I've also tried to add $ at the end of my rewrite rule.

The rewrite engine will loop repeatedly, until the URI stops changing, or the internal redirect limit is reached which causes the 500 error to be thrown. Your rule's target URI /folder/index.php will get thrown back into the rewrite engine and your same rule's regex matches it, ^folder/(.*). So you'll need to add some kind of condition to prevent the loop.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/folder/index\.php
RewriteRule ^folder/(.*) /folder/index.php?Alias=$1 [L]
This is simple, it simply won't apply the rule if it already starts with /folder/index\.php. You can also try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^folder/(.*) /folder/index.php?Alias=$1 [L]
This is a little less restrictive of a condition. It only applies the rule if the requested URI doesn't map to an existing file or directory. This assumes that when you try to go to /folder/blahblah there isn't a directory or file blahblah and that you want to route it through your index.php.

Related

Simple .htaccess rewrite rule doesn't work

I have the following .htaccess file. It refuses to access the following url structure:
www.example.com/test
Even though it accesses this fine:
www.example.com/test.php
I've ran some more complicated rewrite rules using this file and it worked just fine. For example:
RewriteRule ^tests/([0-9]+)/?$ /tests_page.php?id=$1 [PT,L,QSA]
I don't understand how this can be happening. What am I missing here?
#OPTIONS
Options -Indexes
Options +FollowSymLinks
#ACCESS TO THE .HTACCESS FILE
<Files .htaccess>
order allow,deny
deny from all
</Files>
#REWRITE ENGINE
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteEngine On
#PAGES REWRITE
RewriteRule ^test/?$ /test.php
Update:
I renamed the file to tests.php and changed the rule to this and it worked:
RewriteRule ^test/?$ /tests.php
This still does not explain why this is happening though.
Why can't I have the url folder name match the file name?
This appears to be a problem with MultiViews option. Disable it by using:
Options -MultiViews
line at the top of your .htaccess. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite module and makes Apache server match extensions of files. So /file can be in URI but it will serve /file.php.

301 redirect everything to new website root/whole site including sub pages? Moved Permanently

QUICK UPDATE
Ok getting there
Is this mod_alias?
RedirectMatch 301 ^/ http://brightmist.co.uk/
I've added this one line of code underneath everything and it appears to work, however my other directories such as http://brightmist.co.uk/blog/2013/02/23/manchester-art-gallery-feb-2013 are telling google these pages have temporarily moved - see http://www.internetofficer.com/seo-tool/redirect-check/
Does this mean I have to go right the way though my site and add a tone of redirects?
ORIGINAL QUESTION
I have a new website.
I'd like to redirect all of my old site links from http://artygirl.co.uk to my new one http://brightmist.co.uk/
I'm primarily a designer with years of experience using mainly in Photoshop, CSS, HTML, Wordpress, and jQuery but I don't know much about editing things like the htaccess file. And I don't want to get it wrong as it means google ranking drops etc
Does anyone know of any script I can paste into the bottom of my htaccess file, I'd like it to redirect all links/pages on the site to the same place as before. For example if I type http://artygirl.co.uk/buy-art-prints-cheshire/ I want it to go to http://brightmist.co.uk/buy-art-prints-cheshire/ I'm using the same host, they've just re-pointed the domain
Among other things my host has recently added the following code, I assume this is also to do with the domain mapping, also here is my whole htaccess file -
ErrorDocument 401 /forms/401.html
ErrorDocument 403 /forms/403.html
RewriteEngine On
RewriteBase /
#uploaded files
RewriteRule ^(.*/)?files/$ index.php [L]
RewriteCond %{REQUEST_URI} !.*wp-content/plugins.*
RewriteRule ^(.*/)?files/(.*) wp-includes/ms-files.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteCond %{REQUEST_URI} ^.*/wp-admin$
RewriteRule ^(.+)$ $1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-.*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
<IfModule mod_security.c>
<Files async-upload.php>
SecFilterEngine Off
SecFilterScanPOST Off
</Files>
</IfModule>
<Files 403.shtml>
order allow,deny
allow from all
</Files>
deny from 218.143.4.127
deny from 143.90.221.204
deny from 95.135.78.190
deny from 114.108.150.74
deny from 95.135.111.205
deny from 91.124.239.150
deny from 94.178.2.93
deny from 91.124.206.118
deny from 91.124.226.116
deny from 118.98.32.34
deny from 94.180.252.133
deny from 58.27.140.58
deny from 77.93.197.83
deny from 88.191.63.27
# Hotlink Protection START #
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?brightmist.co.uk [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
# Hotlink Protection END #
I'd like to redirect all of my old site links from http://artygirl.co.uk/ to my new one http://brightmist.co.uk/
There are several ways to achieve that, all of them should be implemented in one .htaccess file in http://artygirl.co.uk/ root directory.
There is no need to check for the incoming domain as it must be artygirl.co.uk, where the .htaccess file is located.
To use any of the following options, copy-paste the corresponding directive or rule-set into one empty .htaccess file in http://artygirl.co.uk root directory.
The fastest one is a simple Redirect using one mod_alias directive:
Redirect 301 / http://brightmist.co.uk/
Any path in the incoming URL will be appended automatically to the redirected URL.
To redirect only certain paths using another mod_alias directive:
RedirectMatch 301 ^/(.*) http://brightmist.co.uk/$1
Although this example redirects everything, the regex ^/(.*) can be modified to match only certain URL-path pattern.
To redirect only certain paths using mod_rewrite directives:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) http://brightmist.co.uk/$1 [R=301,NC,QSA,L]
As in the previous option, although this rule-set redirects everything, the regex ^(.*) can be modified to match only certain URL-path pattern.
NOTES
The same directory structure and files in http://artygirl.co.uk/ must exist in http://brightmist.co.uk/ for any of the previous options to work.
If the actual .htaccess file in your question works as expected, you could use it in http://brightmist.co.uk/ root directory where the new WP is installed. Might require some modifications, though.
To move or copy a WP install, check this link Changing the site URL.
UPDATE:
From these sentences in your comment to this answer: "My domains both point at the same directory..." and "...now it creates a loop...", maybe the question is about domains pointing to the same content (Website), normally known as parked domains.
If that's the case, I am not sure redirecting in .htaccess the primary domain to the parked one is the correct approach just to change the domain name in the browser's address bar.
However, in theory something like this should do it using mod_rewrite directives:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} artygirl\.co\.uk [NC]
RewriteRule ^(.*) http://brightmist.co.uk/$1 [R=301,NC,QSA,L]
Redirects permanently any request from http://artygirl.co.uk to http://brightmist.co.uk, appending the complete incoming path and query when present.
Since the .htaccess file is also shared, I think this rule-set should be placed at the top of the .htaccess file in the question, replacing the following lines:
RewriteEngine On
RewriteBase /
Are they redirecting to the same extension? Should be something like this:
RewriteEngine on
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
I see the problem. It redirects the root fine but if I go to:
http://artygirl.co.uk/photography/
I think you want it to go to
http://brightmist.co.uk/photography/
and not
http://brightmist.co.uk/
as it does currently.
Rewrite rules are notoriously hard to debug, but try this...
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^brightmist\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://brightmist.co.uk/$1 [R=301,L]
I know this is really similar to what Matthew Camp answered. The RewriteCond is very important to avoid endless loops because you are delivering both domains from the same folder. You only want to redirect the one that's incorrect.
Rewrite Base might also be required depending on how the servers are configured and what other rewrite rules exist at higher levels (invisible to you but which might still affect you). Try with and without the RewriteBase.
I hope this helps.

Unable to Remove index.php on ExpressionEngine WAMP Installation

I have WAMP v2.2 installed on my Windows 7 machine, with virtual hosts enabled.
I've successfully installed ExpressionEngine for one of my local sites, and everything works great except when I try to remove index.php from the URL using the approved .htaccess method. I still get a 404 error if index.php is not present, but the page displays fine with index.php in the URL.
I made sure the rewrite module was checked in the Apache menu:
. I've successfully used this method dozens of times on commercial hosts, so I'm stumped.
Update for pvledoux:
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# If 404s, "No Input File" or every URL returns the same thing
# make it /index.php?/$1 above (add the question mark)
</IfModule>
Note: I tried adding the ? into the rule, but it didn't work.
The site is at the root of the Virtual Host.
Got It!
I needed to add AllowOverride in httpd-vhosts.conf:
<Directory "C:\Site\Root\Path">
AllowOverride All
Order Deny,Allow
Allow from all
</Directory>
A gagillion thanks and a 6-pack of beer goes to #parhamr for helping me out of this pickle.
A few possibilities here you could try out here:
[...]
RewriteEngine On
Options FollowSymLinks
[...]
Or:
[...]
RewriteEngine On
Options +MultiViews
[...]
Or:
[...]
RewriteEngine On
Options FollowSymLinks
Options +MultiViews
[...]
From my experience, Apache when running on Windows often needs one or both of those in order to do these kinds of things correctly. It might also straight up 500 on you, but these have worked for me in the past.
try to add RewriteBase /
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# If 404s, "No Input File" or every URL returns the same thing
# make it /index.php?/$1 above (add the question mark)
</IfModule>
and check for sure that mod_rewrite is active in phpinfo
Have you checked your default document in Apache? If you are removing index.php physically from the machine and it reports a 404 on just a directory, www.domain.com/home/ that means Apache is looking for index.php still.
Is mod_rewrite turned on in the Apache?

mod_rewrite rule to match exact URL only

I'm having an issue with mod_rewrite where I want to match—and replace—a specific URL. The URL I want to rewrite is:
http://example.com/rss to http://example.com/rss.php
That means, if some one were to append anything after rss a 404 Not Found response be sent. Currently I'm using this mod_rewrite snippet:
Options -Indexes
RewriteEngine on
RewriteBase /
# pick up request for RSS feed
RewriteRule ^rss/?$ rss.php [L,NC]
# pass any other request through CMS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+) index.php/$1
But this matches rss and rss with anything else added to the end. How can I re-write the above to acces only http://example.com/rss as the pattern for mod_rewrite to match against?
You are getting this error because /rss is being redirected twice in your rules by both RewriteRules. Have your rules like this:
Options +FollowSymlinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
# pick up request for RSS feed
RewriteRule ^rss/?$ /rss.php [L,NC]
# pass any other request through CMS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?!^rss\.php$)^(.*)$ /index.php/$1 [L,NC]
So with above rules it will redirect /rss OR rss/ URIs to /rss.php however /rss/foo will be redirected to /index.php since your 2nd rule is forwarding everything to /index.php
I was suprised to see that your rules just don't work, because in my first attempt I would have come to a very similar solution. But looking at the rewrite log revealed the real issue.
As discribed here the server prefers real files over directories. So internally rss/something becomes rss.php/something when applying the rewrite rules and things get weird.
So, one solution is to check if the Option MultiViews is enabled for the web directory either in .htaccess or in the vhost configuration. If so, remove it - which is what worked for me in this example.
If you need MultiViews, then I guess the only chance is to rename rss.php to rss-content.php and change the rule accordingly.
One additional note: you might want to add the following line after the # ... CMS block to prevent endless recursive calls.
RewriteRule ^index\.php/.* - [PT,L]
I hope this solves your rewrite problem.

Need to use httpd.conf instead of .htaccess for rewrite

I am working on a host that blocks my .htaccess file from being used so I can not change my permalinks using it. I need to figure out what code to use and where to put it in my httpd.conf file to get the same effect.
The code in the .htaccess file is below:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
You'll need to wrap that code in a <Directory> directive. Where it goes will depend on what else you've got in your http.conf file. See the Apache docs for more info.
However, as blockhead says; if your host won't let you use .htaccess files, you've got virtually no chance of being allowed near the httpd.conf file.
For example, if you'd like to block access to GoogleBot throughout your entire server (which may be comprised of hundreds of virtual hosts), you can add this to your httpd.conf file:
#setup the root dir
<Directory />
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} Googlebot
RewriteRule .* - [F,L]
</Directory>
This will send a HTTP 403 Forbidden to anyone who comes in with Googlebot in their user agent string. And this rewrite condition will be applied to ALL virtual hosts, by virtue of applying this to the "/" folder.

Resources