Url Rewriting
-
I beginning how to use url rewriting. Now I have this rule in apache (an example) which works fine: RewriteEngine On RewriteRule ^register$ register.php I have also learnt that in order for the rule to accept an ending slash (localhost/register/), the rule should be changed to:
RewriteRule ^register/?$ register.php
This also loads the register.php page when it is accessed through (http://localhost/register) but when I add the trailing slash (http://localhost/register/), my stylesheet which is in a different directory is not applied in formatting the page. The page thus loads without any formatting.
How can I ensure that the stylesheet is used in formatting the page when there is a forward slash at the end? It is possible for a user to add the slash at the end and so I would like to know a solution. Thanks.
-
I beginning how to use url rewriting. Now I have this rule in apache (an example) which works fine: RewriteEngine On RewriteRule ^register$ register.php I have also learnt that in order for the rule to accept an ending slash (localhost/register/), the rule should be changed to:
RewriteRule ^register/?$ register.php
This also loads the register.php page when it is accessed through (http://localhost/register) but when I add the trailing slash (http://localhost/register/), my stylesheet which is in a different directory is not applied in formatting the page. The page thus loads without any formatting.
How can I ensure that the stylesheet is used in formatting the page when there is a forward slash at the end? It is possible for a user to add the slash at the end and so I would like to know a solution. Thanks.
Well I realised that even when I type the actual page in the address bar with the ending forward slash, the page loads without formatting. This means the problem is not with the RewriteRule. Is there a way to solve this problem when the requested page ends in a slash (http://localhost/register.php/) so that the stylesheet is applied? Thanks.
-
Well I realised that even when I type the actual page in the address bar with the ending forward slash, the page loads without formatting. This means the problem is not with the RewriteRule. Is there a way to solve this problem when the requested page ends in a slash (http://localhost/register.php/) so that the stylesheet is applied? Thanks.
You are probably using a relative path to refer to your style sheet. When your URL is
register.php
, that is considered a page. When your URL isregister.php/
, that is considered a directory one level deeper than the directory of register.php. If you use an absolute path, you don't need to worry about that. Example:/css/main.css
Alternatively, you can create a redirect rule (not the same as a rewrite) for when there is a trailing slash and have it redirect to the page without the trailing slash, avoiding the issue entirely.