skip to Main Content

Hey,

I am trying to make my url a bit prettier with apache mod_rewrite.

  • html (root)
    • css
    • js
    • layout
    • sites (folder)
      • profil.php
      • work.php
    • index.php

At the moment my url looks like this:

https://example.com/sites/profil.php

https://example.com/sites/work.php

And I want that it looks like this:

https://example.com/profil

https://example.com/work

How can I can I overwrite the “sites folder” so, there is none and is it possible to remove the .php extension?

Thats how my apache config looks like:

<VirtualHost example.com>
DocumentRoot /var/www/example.com/html
ServerName example.com
ServerAlias www.example.com
<Directory "/var/www/example.com/html">
allow from all
Options None
Require all granted
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Do I need to edit my links after that change in my files?

2

Answers


  1. Chosen as BEST ANSWER
    <VirtualHost example.com>
    DocumentRoot /var/www/example.com/html
    ServerName example.com
    ServerAlias www.example.com
    <Directory "/var/www/example.com/html">
    allow from all
    Options None
    Require all granted
    </Directory>
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =example.com [OR]
    RewriteCond %{SERVER_NAME} =www.example.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
    RewriteRule ^sites/profil.php$ /profil?&%{QUERY_STRING}
    RewriteRule ^sites/work.php$ /work?&%{QUERY_STRING}
    </VirtualHost>
    

  2. Here are your mod rewrite rules

    # Rewrite --- https://example.com/sites/profil.php => https://example.com/profil
    RewriteRule ^profil$ sites/profil.php [NC,L]
    
    # Rewrite --- https://example.com/sites/work.php => https://example.com/work
    RewriteRule ^work$ sites/work.php [NC,L]
    

    enter this into your .htaccess in the right location and give it a try.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search