1

Hoping one of you can help. I am developing using php, without a framework, therefore all of my routing rules are in .htaccess as opposed to .php

So here is my problem. Say my website is http://example.com and my navigation is pretty straight forward, meaning if you click the 'press' page link you will go to http://example.com/press or http://example.com/press/ which are both fine.

However, if you do happen to go to http://example.com/press/ click another internal link in my nav, such as 'about' etc.. I will not take you to '/example' but rather '/press/example' which shows a 404 since there is not page or directory to handle this.

I tried 'DirectorySlash Off' in my .htaccess but all that did was prevent any of these pages from showing if a url has / at the end.

I have a very basic .htaccess setup, See below:

<IfModule mod_rewrite.c>

RewriteEngine On
# Options -Indexes
# GEN CONFIG



# Handle Errors
ErrorDocument 403 /views/errors/404.php
ErrorDocument 404 /views/errors/404.php
ErrorDocument 303 /views/errors/404.php

# Url renamin
RewriteRule ^(about-us|About-Us|ABOUT-US)/?$ views/about-us.php [L]

RewriteRule ^(press|Press|PRESS)/?$ views/press.php [L]
RewriteRule ^(faq|Faq|FAQ)/?$ views/faq.php [L]

</IfModule>
VickenCode
  • 305
  • 2
  • 12

1 Answers1

1

go to http://example.com/press/ click another internal link in my nav, such as about etc.. I will not take you to /about but rather /press/about

This problem is not due to rewrite rules but due to your use of relative paths.

To fix, you can add this just below <head> section of your page's HTML:

<base href="/" />

so that every relative URL is resolved from that base URL and not from the current page's URL.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Although I thought the same thing, and tried that method earlier, it keeps appending the next link to the trailing slash at the end. – VickenCode Jul 25 '16 at 14:16
  • Also just to further my point, if you go to http://example.com/press/ and click another link it just appends it to the end of the trailing slash. – VickenCode Jul 25 '16 at 14:19
  • Yes that is what the behavior will be if you use relative links instead of absolute ones. Absolute paths start either with `http://` or a slash `/`. – anubhava Jul 25 '16 at 14:22
  • Yes you are correct, it seems './' or just the page name like 'about-us' was causing this, however as soon as I added '/about-us' it was resolved.. However I dont know why some page urls will still accept a trailing slash at the end but other wont? – VickenCode Jul 25 '16 at 14:29
  • All of your rules have optional trailing slash so there shouldn't be any problem of having or not having it. `404` while clicking next **valid** link is always due to relative paths being used. – anubhava Jul 25 '16 at 14:31
  • 1
    yes it is thank you! I must improve my REGEX abilities in order to need less help understanding these .htaccess files. You have been an Angel! – VickenCode Jul 25 '16 at 18:10