0

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
  • 1
    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. Commented Jun 1, 2011 at 18:33
  • @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. Commented Jun 1, 2011 at 18:48

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

6 Comments

I have already tried this but don't work. After url/ i need to uset http: //domain.tld and this don't work.
A) I don't understand what you mean by "uset http: //domain.tld and this don't work", and B) it would be nice if you had included what you have already attempted so we don't waste our time suggesting things that you've already tried. The principles in this snippet of htaccss work - I took it straight out of a working htaccss file on my own site. :) Edit your question and include ALL the relevant details, including what you have already tried.
I have a link like this api.php?uid=1&args=1&url=http: //domain.tld (without space between http: and //) and I want to rewrite this link but with ^id/(.*)/type/(.*)/url/(.*)$ the last syntax don't work, my php don't get $3. Without mod_rewrite all is fine, but with mode rewrite don't work $3 (&url=$3). I don't know how to explain more.
The trouble might be that it isn't valid to pass a non-url-encoded URL as a query string parameter. That url= portion needs to be encoded, so http://domain2.tld needs to be http%3A%2F%2Fdomain2.tld. The reason that the mod_rewrite rule is failing is because of the unencoded forward slashes. See also: php.net/manual/en/function.urlencode.php
I understand but look at this, they can do that: zpag.es/api/4947102a15cb93454e1c7d76c130f83a/uid/5892/domain/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.