1

I have a url of the form:

www.example.com/r/abc/1234

that I want to rewrite to:

www.example.com/r.php?deb=abc&id=1234

And another url:

www.example.com/r/12bcd-qsqs-343-wdwd/1234

which should be rewritten to:

www.example.com/r.php?abc_id=12bcd-qsqs-343-wdwd&id=1234

Both of these rule should be handled by .htaccess. Any suggestions?

1
  • Pass all into a script of your own and than inside that PHP script process the URI as one parameter. That is much more less specific to the webserver you are running on and more flexible. This normally only needs a single or very few rules. Compare: symfony2 rewrite rules .htaccess app.php Commented Aug 28, 2012 at 7:53

2 Answers 2

1

Both the URIs that you want to redirect look similar except that 2nd one has hyphens - in first query parameter.

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

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

RewriteRule ^r/([^-]+)/(.+)/?$ r.php?deb=$1&id=$2 [L,QSA]

RewriteRule ^r/([a-z0-9-]+)/(.+)/?$ r.php?abc_id=$1&id=$2 [L,QSA,NC]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the help with little changes I have made it work, But now the problem is its working on my localhost but when I transferred it on the server were I am running virtualhost Its not working can you help ?
On the server make sure .htaccess is enabled and then if .htaccess is correctly paced in DOCUMENT_ROOT directory. If everything is right then add R flag in above 2 RewriteRule lines to see what is the final URI on server.
Thanks a lot :) Is there some how I can keep contact with you any social site or anything ?
You're welcome. Here is LinkedIn Contact: linkedin.com/in/anubhavasrivastava
1

Try something like :

RewriteRule (.*)/(.*)/(.*)$ path_to_php_files/$1.php?deb=$2&id=$3 [QSA,L,NC]

Unfortunately with this rule, you would have the first $_GET parameter always deb and I hope you have the DirectoryIndex set up corectly. I do not think that for

www.example.com/r/abc/1234
www.example.com/r/12bcd-qsqs-343-wdwd/1234

you can delivrer the abc and 12bcd-qsqs-343-wdwd to 2 different $_GET parameters if you have dynamic links.

2 Comments

Won't work because RewriteRule doesn't match starting slash /.
Actually if you notice OP wants to redirect in 2 different ways using 2 different query parameters.

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.