1

So I'm trying to use url rewriting to simplify url's on a website.

Example usage:

www.example.com/test -> www.example.com/index.php?page=test
www.example.com/test/x -> www.example.com/index.php?page=test&value=x

I use this for the rewriting:

RewriteEngine on

#Simplify url
RewriteRule ^(\w+)/?$ index.php?page=$1
RewriteRule ^test/(\w+)/?$ index.php?page=test&value=$1

The issue is that this approach seems to preserve the location prior to the rewrite. So other files loaded on the website like CSS are relative to the original location rather than the location after being rewritten.

Example:
www.example.com/test/x rewrites to
www.example.com/index.php?page=test&value=x.

But CSS files loaded when a user enters www.example.com/test/x are loaded relative to the /test/ folder rather than the / folder. So they're not found.

Am I doing something incorrectly? I'd assumed that rewriting would literally redirect, so things like this wouldn't be an issue. I'd like to solve this issue rather than just using absolute url's for everything - so I can still use it on my test server.

2 Answers2

0

1)

There is nothing wrong using absolute URLs. You can use define.

For the development environment use:

define('BASE_URL', 'http://test.server.local/');

then for the production environment just change it to:

define('BASE_URL', 'http://www.example.com/');

On all the pages of Your code, You can access those URLs as

<a href="<?=BASE_URL.'test/x'?>">x page</a>

So You don't need to change code on all the pages where You reference the BASE_URL

2)

It is a good idea to place all your css in the styles/ directory, then in .htaccess file you can exclude it from rewriting like that:

RewriteEngine on

# add this line:
RewriteRule ^/?styles/.+$ - [L]

#Simplify url
RewriteRule ^(\w+)/?$ index.php?page=$1
RewriteRule ^test/(\w+)/?$ index.php?page=test&value=$1

UPDATE (regarding Your comment)

If you use the concept of BASE_URL the correct URL is made on server and then passed to the browser. If you use <base> you depend on the client side (browser the user uses). It is a good practice to use BASE_URL on the server side, thus you won't depend on the client's browser.

Check out this answer: Is it recommended to use the base html tag?

You can also include the php file (that has the define() function) to all your pages, thus there is no need to use <base> on every page. Here is a nice example of using this.

Community
  • 1
  • 1
Dmytro Dzyubak
  • 1,562
  • 2
  • 22
  • 35
0

It's important to keep in mind that rewriting is not the same as redirecting. The browser doesn't know about the rewrite that is happening; it just sees the folder structure as it seems.

Relative URLs for site resources are resolved by the browser. So, if you access www.example.com/test/x, and the browser sees <link href="style.css">, it naturally reads this as www.example.com/test/x/style.css, and tries to request this file, only to receive a 404.

One common solution is to always use absolute URLs like www.example.com/style.css. You would most likely store your site's URL as a constant and use <link href="<?php echo SITE_URL; ?>/style.css">.

wavemode
  • 2,076
  • 1
  • 19
  • 24