Вопрос

Setup: I own a regular Unix Webserver running Apache and PHP. It is serving PHP scripts as expected.

Current Situation: All my links look like typical PHP ones with parameters website.net/index.php?show=article&do=new

What i want: a mod_rewrite rule for remapping my urls, no matter how they look or contain.

For example: website.net/article/new should redirect to index.php and $_SERVER['REQUEST_URI'] should contain /article/new/ etc. but not limited to that and no fixed pattern (*n variables), all the other work is done with PHP (validating, including).

My effort this far: used google a lot and tried many expamples. but only found some with fixed patterns and some did even break the whole site.

The other problem: i noticed while experimenting with mod_rewrite that it broke the path to my js and css files. I use relative pathes, they are located in a subfolder relative to index.php

<script src="js/included_file.js"></script>

<link href="css/included_file.css" rel="stylesheet"">

Это было полезно?

Решение

You can use the -f RewriteCondition to exclude files:

RewriteCond %{REQUEST_FILENAME} !-f          #exclude files
RewriteRule ^(.*) index.php?__path=$1 [QSA]  #catch everything

If you want to keep directories too you can do that with -d.

Now if you call the URL:

website.net/path/to/file?query=string&foo=bar

You will get (in your $_GET):

__path = "/path/to/file"
query = "string"
foo = "bar"

Don't use $_REQUEST. It is non-standard and mixes $_GET and $_POST, it can lead to very strange bugs.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top