-2

how to correctly write a .htaccess file to redirect links on my site. I have such a links:

http://mysite/todo/1001/
http://mysite/todo/1002/
http://mysite/todo/1003/

and I want to redirect them all to

http://app.mysite/todo/1001/
http://app.mysite/todo/1002/
http://app.mysite/todo/1003/

Thank you!

bars38
  • 21
  • 1
  • possible duplicate of [How to redirect in apache?](http://stackoverflow.com/questions/11064205/how-to-redirect-in-apache) – Sumurai8 Jun 02 '14 at 13:48
  • Please create a minimal example. Show us the code for that example, what error you get and what you expected to happen. This shows that you have put effort into it, as well as that you have a basic understanding of the problem you are trying to solve. Questions in the form of "please write my code" are a bad fit for StackOverflow. – Sumurai8 Jun 02 '14 at 13:55

1 Answers1

0

Place this in .htaccess:

RewriteEngine On 
RewriteRule   ^todo(.*)$ http://app.mysite/todo$1 [R=301,NC,L]

As the code states it enables the Apache RewriteEngine (RewriteEngine On). The apache RewriteEngine allows for a RewriteRule to execute wich initiates a redirect.

The RewriteRule takes 2 parameters. The first parameter is written between ^ and $. It contains a reference to the URL that needs catching, catched by a string (todo) and (.*) which takes all trailings behind the "todo".

The second parameter takes the redirect URL. The $1 takes the trailing paramaters found by the first parameter.

Then the RewriteRule can have flags (R=301, NC, L). If the url move is permanent R=301 should be appended. NC tells apache to treat the condition as case-insensitive. Appending flag L tells apache not to execute any further rules below the current RewriteRule.

neuzehie
  • 152
  • 1
  • 15
  • But if I have a lot of such links? Is it possible to redirect them all with one rule? – bars38 Jun 02 '14 at 13:40
  • Try out `RewriteRule ^http://mysite/todo/([0-9]+)/?$ http://app.mysite/todo/$1 [R=301,NC,L]` – neuzehie Jun 02 '14 at 14:00
  • Do you have the RewriteEngine On statement above the RewriteRule? – neuzehie Jun 02 '14 at 14:11
  • I updated the solution without first domain calls: Try this: `RewriteEngine On RewriteRule ^todo(.*)$ http://app.mysite/todo$1 [R=301,NC,L]` – neuzehie Jun 02 '14 at 14:18
  • RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] RewriteRule ^signup/(.*)$ http://app.mysite/signup/$1 [R=301,NC,L] – bars38 Jun 02 '14 at 14:30
  • This post is being automatically flagged as low quality because it is only code. Would you mind expanding it by adding some text to explain how it works / how it solves the problem? – gung - Reinstate Monica Jun 02 '14 at 14:31