0
votes

I want to use mod_rewrite to do urls like this:

http://domain.tld/id/1/type/2/url/http://domain2.tld

How can I do that?

2
What do you want the url to become after the re-write? This is critical information to know how to craft the mod_rewrite rule. - Chris Baker
@Chris I have an api like this: api.php?uid=1&args=1&url=domain.tld and I want to use mod_rewrite for this. - Alexander

2 Answers

1
votes

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.

0
votes

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.