2

I am creating a CMS in which there are three types of rendering pages:

1 - https://test-site.org/whatever-title

2 - https://test-site.org/test-page.php

3 - https://test-site.org/category/whatever-category

Originally I only had the first two and the below htaccess work fine

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?title=$1 [QSA,L]

RewriteCond %{HTTP_HOST} test\.org [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.test.org/$1 [R,L]

However, when I added the third (category) with the rewrite rule for the third url type htaccess does not recognize the third url type but still sees and renders the first two correctly:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?title=$1 [QSA,L]

RewriteRule ^category/(.*)$ category.php?title=$1 [QSA,L]

RewriteCond %{HTTP_HOST} s8w\.org [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.s8w.org/$1 [R,L]

If I swap positions for RewriteRule ^(.*)$ index.php?title=$1 [QSA,L] and RewriteRule ^category/(.*)$ category.php?title=$1 [QSA,L] htaccess only recognizes the second and third url types.

I tried the below code which recognizes the first and second url types

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?title=$1 [QSA,L]

RewriteRule ^category/(.*)$ category.php?title=$1 [QSA,L]

RewriteCond %{HTTP_HOST} s8w\.org [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.s8w.org/$1 [R,L]

but throws the following error on the third url type: [an error occurred while processing this directive]

I am self-taught and have no idea as to why the code doesn't work... Your assistance is much appreciated.

petebolduc
  • 1,233
  • 1
  • 13
  • 20

1 Answers1

1

Have it this way:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /

# http -> https
RewriteCond %{HTTP_HOST} s8w\.org [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

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

RewriteRule ^category/(.*)$ category.php?title=$1 [QSA,L,NC]

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

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

Make sure to test it from a new browser to avoid old cache.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • the code does work... it is recognizing the title and I am able to parse from the db but for some reason the css file is no longer being called – petebolduc Apr 04 '19 at 21:25
  • css/js/image is a well known issue after URL rewrite. To fix, you can add this just below `` tag of your page's HTML: `` of your HTML. – anubhava Apr 04 '19 at 21:28
  • 1
    thanks... that did the trick... you have helped me in the past with htaccess and I was hoping you would stumble across this one... thanks a bunch – petebolduc Apr 04 '19 at 21:34
  • ever since I implemented the htaccess I have been having issues with the variable in included files assuming their values... I was wondering if you could take a look at my related question: https://stackoverflow.com/questions/55667444/htaccess-mod-rewrite-preventing-variables-from-assuming-their-values-on-included – petebolduc Apr 16 '19 at 13:47