Tom
Tom

Reputation: 25

URL Rewriting on Apache server causes 404 error page

A few weeks ago I was trying url rewriting on my shared host. It was working for a while, but nowadays it seems to be broke. I don't know what the problem is and I can't solve it by myself. Hope you could help, guys.

This is the plan. I want to delete the extension in the url (e.g. 'file.html' or 'file.php') to only the filename (e.g. 'file'). My code is the following:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule ^index/(.*)/$ index.php?page=$1
  RewriteRule ^([^\.]+)$ $1.php [NC,L]
  RewriteRule ^([^\.]+)$ $1.html [NC,L] 
  ErrorDocument 404 404.php
  ErrorDocument 403 403.php
  ErrorDocument 401 401.php
</IfModule>

One extended note: this .htcaccess file is in the directory 'portfolio'. The root has another .htcaccess file which is the following:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase / 
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . index.php [L]
</IfModule>

Maybe it is something obvious, but I couldn't find what the problem is. I did try 'RewriteBase /website/', 'RewriteBase /', 'RewriteCond %{REQUEST_FILENAME} !-f' and 'RewriteCond %{REQUEST_FILENAME} !-d' in the first .htaccess file, but that doesn't work.

Upvotes: 1

Views: 2298

Answers (1)

anubhava
anubhava

Reputation: 784878

You can have these rules in /portfolio/.htaccess

ErrorDocument 404 404.php
ErrorDocument 403 403.php
ErrorDocument 401 401.php

RewriteEngine On
RewriteBase /portfolio/

RewriteRule ^index/(.+?)/$ index.php?page=$1 [L,QSA]

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

RewriteRule ^([^.]+)$ $1.php [L]    

Your rules for adding .php or .html need to have a RewriteCond to check whether corresponding files exist in your portfolio directory.

Upvotes: 1

Related Questions