Search code examples
apache.htaccessmod-rewriteurl-rewritingclean-urls

How to refactor my .htaccess to get a get semantic urls for ALL my pages


.htaccess files really aren't my thing. They still remain a mystery to me and probably will always be...

Anyway, so I have an .htaccess file with quite some rewrite rules for all different pages.

URL: http://placeholder.com/OC/hoig/[page]

Folder structure:

The goal is to get semantic (pretty) URL's for all pages. Right now, this .htaccess file works partly. Almost all pages get a pretty URL, except 'Leerinhoud detail'. For some reason, it returns content from 'Leerinhoud' but under a pretty url (http://placeholder.com/OC/hoig/leerinhoud/1/the-slug).

Also, I'm not so sure about the [R=302,L] flag because that's a temporary redirect, which I don't really need.

GOAL

http://placeholder.com/OC/hoig/index.php to http://placeholder.com/OC/hoig/ http://placeholder.com/OC/hoig/leerinhoud.php to http://placeholder.com/OC/hoig/leerinhoud http://placeholder.com/OC/hoig/leerinhoud-detail.php?id=1&leerinhoud=the-slug to http://placeholder.com/OC/hoig/leerinhoud/1/the-slug
...

.htaccess:

RewriteEngine On
RewriteBase /OC/hoig/

RewriteCond %{THE_REQUEST} /news-detail\.php\?id=([^\s&]+)&news=([^\s&]+) [NC]
RewriteRule ^ nieuws/%1/%2? [R=302,L]
RewriteRule ^nieuws/([^/]+)/([^/]+)/?$ news-detail.php?id=$1&news=$2 [L,NC,QSA]

RewriteCond %{THE_REQUEST} /leerinhoud\.php [NC]
RewriteRule ^ leerinhoud [R=302,L]
RewriteRule leerinhoud leerinhoud.php [L,NC,QSA]

RewriteCond %{THE_REQUEST} /leerinhoud-detail\.php\?id=([^\s&]+)&leerinhoud=([^\s&]+) [NC]
RewriteRule ^ leerinhoud/%1/%2? [R=302,L]
RewriteRule ^leerinhoud/([^/]+)/([^/]+)/?$ leerinhoud-detail.php?id=$1&leerinhoud=$2 [L,NC,QSA]

RewriteCond %{THE_REQUEST} /partners\.php [NC]
RewriteRule ^ partners [R=302,L]
RewriteRule partners partners.php [L,NC,QSA]

RewriteCond %{THE_REQUEST} /contact\.php [NC]
RewriteRule ^ contact [R=302,L]
RewriteRule contact contact.php [L,NC,QSA]

# RewriteCond %{THE_REQUEST} /search\.php\?q=([^\s&]+) [NC]
# RewriteRule ^ search-results/%1? [R=302,L]
# RewriteRule search-results/([^/]+)/?$ search.php?q=$1 [L,NC,QSA]

RewriteCond %{THE_REQUEST} /rss-detail\.php\?rss=([^\s&]+) [NC]
RewriteRule ^ rss/%1? [R=302,L]
RewriteRule rss/([^/]+)/?$ rss-detail.php?rss=$1 [L,NC,QSA]

RewriteCond %{THE_REQUEST} /tags\.php [NC]
RewriteRule ^ tags [R=302,L]
RewriteRule tags tags.php [L,NC,QSA]

RewriteCond %{THE_REQUEST} /tag\.php\?tag=([^\s&]+) [NC]
RewriteRule ^ tag/%1? [R=302,L]
RewriteRule tag/([^/]+)/?$ tag.php?tag=$1 [L,NC,QSA]

RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^ nieuws [R=302,L]
RewriteRule nieuws index.php [L,NC,QSA]

TL;DR: How can I get some kickass clean SEO-friendly URL's for all my pages?

Thanks in advance.


Solution

  • You have 2 solutions:

    First is to swap your rules order

    RewriteCond %{THE_REQUEST} /leerinhoud-detail\.php\?id=([^\s&]+)&leerinhoud=([^\s&]+) [NC]
    RewriteRule ^ leerinhoud/%1/%2? [R=302,L]
    RewriteRule ^leerinhoud/([^/]+)/([^/]+)/?$ leerinhoud-detail.php?id=$1&leerinhoud=$2 [L,NC,QSA]
    
    RewriteCond %{THE_REQUEST} /leerinhoud\.php [NC]
    RewriteRule ^ leerinhoud [R=302,L]
    RewriteRule leerinhoud leerinhoud.php [L,NC,QSA]
    

    Second is to put begin/end of pattern for leerinhoud (this way, order of your rules won't matter anymore)

    RewriteCond %{THE_REQUEST} /leerinhoud\.php [NC]
    RewriteRule ^ leerinhoud [R=302,L]
    RewriteRule ^leerinhoud$ leerinhoud.php [L,NC,QSA]
    
    RewriteCond %{THE_REQUEST} /leerinhoud-detail\.php\?id=([^\s&]+)&leerinhoud=([^\s&]+) [NC]
    RewriteRule ^ leerinhoud/%1/%2? [R=302,L]
    RewriteRule ^leerinhoud/([^/]+)/([^/]+)/?$ leerinhoud-detail.php?id=$1&leerinhoud=$2 [L,NC,QSA]
    

    Conclusion
    Understand that RewriteRule leerinhoud leerinhoud.php [L,NC,QSA] and RewriteRule ^leerinhoud$ leerinhoud.php [L,NC,QSA] are not the same. First one will always match because leerinhoud can be everywhere in your url (like leerinhoud.php or leerinhoud/something/slug).

    Note: you may also have to disable MultiViews option when you rewrite filename without extension

    Options -MultiViews