-1

I wish I could have SEO friendly navigation links like example.com/about or example.com/login or example.com/register but my website's links are like example.com/public/pages/about.php, example.com/public/pages/login.php etc. I tried doing it using this code I found on the internet:

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /public/pages/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]

RewriteRule ^((?!public/pages/).*)$ public/pages/$1 [L,NC]

the code worked fine by making them example.com/login.php but the problem is that the pages are showing up unstyled (without CSS).

This is my navigation:

                <li class=""><a href="index.php">Home</a></li>
                <li><a href="./public/pages/request.php">Requests</a></li>
                <li><a href="./public/pages/courses.php">Programs</a></li>
                <li><a href="./public/pages/trending-tut.php">Tutorials</a></li>
                <li><a href="public/pages/blog.php">Discussion Room</a></li>
                <li><a href="./public/pages/agriculture.php">Agri-Business</a></li>
              </ul>
              <ul class="nav navbar-nav navbar-right">
                
                <li><a href="./public/pages/register.php"><span class="glyphicon glyphicon-user"></span> Register</a></li>
                <li><a href="./public/pages/login.php"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
              </ul>```
İsmail Y.
  • 3,579
  • 5
  • 21
  • 29
Isaacs
  • 1
  • 2
  • Does this answer your question? [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](https://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained) – ArSeN Jan 18 '21 at 21:04
  • Why not fix the styling? What have you tried to achieve this? – Nico Haase Jan 18 '21 at 21:31
  • @Nico Hasse... I am new to mod_rewrite so I dont know how I can fix that.... and my styles are in public/assets/css/styles.css... – Isaacs Jan 19 '21 at 09:18

1 Answers1

0

This would be the generic approach to internally rewrite any path to which a corresponding php file exists in that folder:

RewriteEngine On
RewriteBase /
RewriteCond /public/pages%{REQUEST_URI}.php -f
RewriteRule ^ /public/pages%{REQUEST_URI}.php [QSA,END]

Another approach would be to explicitly list those short URLs you want to use one by one:

RewriteEngine On
RewriteBase /
RewriteRule ^/?about$ /public/pages/about.php [QSA,END]
RewriteRule ^/?login$ /public/pages/login.php [QSA,END]
RewriteRule ^/?register$ /public/pages/register.php [QSA,END]

This obviously means more maintanance effort, but preserves freedom.

You could also try to redirect clients that still use the long notation:

RewriteEngine On
RewriteBase /

RewriteRule ^/?public/pages/(.+)\.php$ /$1 [QSA,R=301,END]

RewriteCond /public/pages%{REQUEST_URI}.php -f
RewriteRule ^ /public/pages%{REQUEST_URI}.php [QSA,END]

You need to test this with a fresh anonymous browser window to prevent getting fooled by cached results. And you should start out with a 302 redirection and only change that to a 301 once you are convinced your solution is final.


In general you should prefer to implement such rules in the real http server's host configuration. Distributed configuration files (".htaccess") are only a last option if you have no access to the real configuration. They come with disadvantages though and you need to take care that they are considered at all in your http server.

arkascha
  • 41,620
  • 7
  • 58
  • 90