2

I am trying to create fake directories to redirect specific parameters to specific subdirectories as such:

From: www.example.com/username
  To: www.example.com/posts?uid=alex

From: www.example.com/p/1234
  To: www.example.com/posts?url=1234

It is somehow more complicated example from the rest questions with the same subject on stackoverflow and most answers do not give an explanation on how it works, thus I am not able to come into a conclusion on how I could solve this.

4

2 回答 2

3

Try:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/([^/]+)$ /posts?url=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /posts?uid=$1 [L]

First, you need to make all of your links look like this:

www.example.com/username
www.example.com/p/1234

The rewrite rules above will match the /p/1234 (via ^p/([^/]+)$) and /username (via ^([^/]+)$) and internally rewrite them to the /posts URI. Note that this will not change the URL in the browser's location bar, as that is an external redirect. If someone enters www.example.com/posts?uid=alex, the URL will be unchanged.

于 2013-10-03T06:12:25.333 回答
1

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^p/([^/]+)/?$ /posts?url=$1 [L,QSA,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /posts?uid=$1 [L,QSA,NC]
于 2013-10-03T06:12:42.827 回答