1

Currently i am trying to get url rewriting to work on one.com hosting provider which is a pain and support don't understand the issue, so i was thinking of asking here if you may assist me finding the issue.

I'm using this rewriterule which worked on localhost but not with the provider:

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?key=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?key=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?key=$1&seo=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?key=$1&seo=$2

</IfModule>

RewriteCond %{HTTP_HOST} ^mysite\.com
RewriteRule ^(.*)$ http://www\.mysite.com/$1 [R=permanent,L]

and in index:

if($key=='post') //Cant include $seo here because the variable differ for 
                 //each post on the site
{
include('post.php'); // Post page
}

Then in post.php ofc i take the seo GET variable to display the post content on the page.

complete url www.mysite.com/post/test-test-test

The error i end up with is 404

Not Found

The requested URL /post/test-test-test.php was not found on this server.

So basically what i understand is that it tries to enter a folder named /post/ and look for the file test-test-test.php

So i believe it doesn't include the rewrite rule, or can you find an error in the rewrite rule which worked on localhost?

Jim Sundqvist
  • 160
  • 2
  • 17
  • Do you have more rules in your .htaccess? – anubhava Aug 25 '14 at 03:28
  • You are correct in the assumption that no rewrite is being done, as it would show `index.php was not found on this server` otherwise. See http://stackoverflow.com/questions/869092/how-to-enable-mod-rewrite-for-apache-2-2 for more information. – Sumurai8 Aug 25 '14 at 06:35
  • @Sumurai8 That site seems to be about enabling it on localhost where you have access to the server. I'm trying to get it to work on my hosting provider where i have no access at all to the server. – Jim Sundqvist Aug 25 '14 at 11:04
  • Then follow the steps that do not require access to httpd.conf and see if it works. If it doesn't, you have to contact your hosting provider. – Sumurai8 Aug 25 '14 at 11:14

1 Answers1

1

You need to rearrange your rules and for adding .php extension make sure a file with .php exists:

<IfModule mod_rewrite.c>

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,NE]

# ignore rest of the rules for files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^([\w-]+)/?$ index.php?key=$1 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?key=$1&seo=$2 [L,QSA]

RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

</IfModule>
anubhava
  • 761,203
  • 64
  • 569
  • 643