Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple .htaccess rewrite not working on GoDaddy

My company is stuck with GoDaddy for now, and my .htaccess rewrite isn't working. This works fine on my localhost.

The intent is to have example.com/about actually get example.com/about.php, with the URL still just showing example.com/about.

Here's my .htaccess file:

Options +FollowSymlinks -MultiViews -Indexes

RewriteEngine on

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

I have read all the other posts about inconsistent .htaccess behavior with GoDaddy servers. I did have them confirm that mod_rewrite is enabled, and my PHP is 5.4.19. Hopefully someone has been through this already or can shed some light...

like image 581
pyro_lemur Avatar asked Jun 17 '15 03:06

pyro_lemur


4 Answers

You should simplify your rule. Go Daddy is annoying but this should work.

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /$1.php [L]
like image 196
Panama Jack Avatar answered Oct 05 '22 23:10

Panama Jack


This has come up quite a few times - GoDaddy is synonymous with problems such as these. I recommend the following:

First, place this at the top of your .htaccess file:

RewriteEngine On

# Remove php extension from the request by means of a redirect.
# This should really be for php files that exist
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ /%1 [R,L,NC]

Then try one of the following after the above:

# 1. Map the request to a PHP file, if one exists
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
# 2. Map the request to a PHP file, if one exists (alternative method)
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]
# 3. Map the request to a PHP file, provided the request is not
#    a file or a directory. The PHP file in question does not need
#    to exist on the file system.
RewriteCond %{REQUEST_FILENAME} !-f    
RewriteCond %{REQUEST_FILENAME} !-d    
RewriteRule ^(.*)$ /$1.php [L]    

The issue at hand may be in relation to the Options directive. From past experience, it generally comes from using Includes in this directive - I understand that GD doesn't allow these. However, you are not using them, so try any of the following combinations to see if the 500 error doesn't appear.

Options +FollowSymLinks -Multiviews
Options +FollowSymLinks
Options -Multiviews

GD turns off Indexing by default, and so it is not necessary to use that in the Options directive.

Side note: You may already know this, but don't use php_value and php_flag in .htaccess files on GD hosting. Instead, you would need to create a php5.ini file with the values and flags set in there.

Update: I have remove RewriteBase (as it has been known to cause issues for some users) and made the redirects/rewrites relative to the root of the domain.

like image 21
Mike Rockétt Avatar answered Oct 06 '22 00:10

Mike Rockétt


Your server must be Linux for .htaccess to work. On GoDaddy servers it will only work on Linux hosting and not Windows hosting. For Windows hosting, you have to configure web.cong to enable rewrite rules.

like image 20
Amarprem Cool Avatar answered Oct 05 '22 23:10

Amarprem Cool


I finally figured this one out. Thanks again to everyone who took the time to help. There was another .htaccess file in the root directory of my hosting account that was causing the 500 error, which I removed. I then got to a 400 error, which I was able to resolve using RewriteBase. Here is the working .htaccess file:

RewriteEngine On
RewriteBase /this_sites_subdirectory/
RewriteRule ^/?$ /1 [L]

Options -MultiViews                

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php 
like image 29
pyro_lemur Avatar answered Oct 06 '22 01:10

pyro_lemur