PixelFactory Austria PixelFactory Austria - 4 months ago 23
Apache Configuration Question

HTACCESS Conditions and/or Rewrites are NOT working

I have a Problem with my root .htaccess file.

That's my .htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

# START
RewriteRule ^$ index.php [QSA,L]

# PAGE
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{HTTP_HOST} !category
RewriteRule ^([A-Za-z0-9_-]+)/?$ index.php?page=$1 [QSA,L]

# POST
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{HTTP_HOST} !category
RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/?$ index.php?page=$1&post=$2 [QSA,L]

# CATEGORY
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{HTTP_HOST} category
RewriteRule ^category$ category.php [QSA,L]

# SINGLE CATEGORY
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^category/([A-Za-z0-9_-]+)/?$ category.php?category=$1 [QSA,L]

# XML SITEMAP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sitemap\.xml$ sitemap.php [L]
</IfModule>


When I try to go to


www.pixel-factory.at/category


as a result I get a empty "page". This means it goes to


www.pixel-factory.at/index.php?page=category


(which don't exists) instead of going to


www.pixel-factory.at/category.php


Please someone help, can't get it done. Thank you!

Answer

You can refactor your conditions to shorter your code, remove RewriteCond %{HTTP_HOST} category condition everywhere and reorder your rules to keep generic rules in the end:

DirectoryIndex index.php
RewriteEngine on
RewriteBase /

# XML SITEMAP
RewriteRule ^sitemap\.xml$ sitemap.php [L]

# skip all rules from existing files or directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# CATEGORY
RewriteRule ^category/?$ category.php [NC,L]

# SINGLE CATEGORY
RewriteRule ^category/([\w-]+)/?$ category.php?category=$1 [NC,QSA,L]

# search term
RewriteRule ^search/([\w-]+)/?$ search.php?search=$1 [NC,QSA,L]

# PAGE
RewriteRule ^([\w-]+)/?$ index.php?page=$1 [QSA,L]

# POST
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?page=$1&post=$2 [QSA,L]
Comments