Rewriting seo-url to url with query string
I want to use mod_开发者_StackOverflowrewrite to do urls like this:
http://domain.tld/id/1/type/2/url/http://domain2.tld
How can I do that?
Put this code in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/+id/([^/]*)/type/([^/]*)/url/(http://)?(.*)$ [NC]
RewriteRule ^ /api.php?uid=%1&type=%2&url=%3%4 [L,NE]
This will support both /id/1/type/2/url/http://domain2.tld
and /id/1/type/2/url/domain2.tld
URIs.
Now we're in business! Create an .htaccess
file, then add these lines:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^id/(.*)/type/(.*)/url/http:/(.*)$ api.php?uid=$1&type=$2&url=$3 [L]
Note that this will end up passing domain2.tld
as the url
parameter - you'll have to add the "http://" back on yourself. As we've discussed in comments, you're better off with properly formed URLs using urlencode
, but if that isn't an option, this will do.
There is no dearth of information on mod_rewrite
on the internet. Here's a blog: http://www.htmlist.com/how-to/a-simplemod_rewrite-tutorial/ - just one of many results if you search for "mod_rewrite" on the Google.
精彩评论