3

Ok, so I have a page.php placed on www.example.com/test that I want to rewrite to www.example.com/test/page/id ( from www.example.com/test/page.php?page=id )

I added this simple .htaccess and everything's fine so far

Options +FollowSymLinks
RewriteEngine on
RewriteRule page/(.*)/ page.php?page=$1
RewriteRule page/(.*) page.php?page=$1

The thing though is that I also want to redirect example.com/test/page/id to www.example.com/test/page/id

Is this possible to do? I tried adding a RewriteCond but it messed it up more.

Thanks in advance

5
  • Possible duplicate of Redirect non-www to www in .htaccess
    – Sylwit
    Aug 26, 2016 at 3:18
  • That post doesn't talk about dynamic pages... Aug 26, 2016 at 11:48
  • It's 2 concepts. First always redirect your non www to www with a 301 then your dynamic rewrite will work as you mentioned it.
    – Sylwit
    Aug 26, 2016 at 11:50
  • Yeah but if I put a rewrite cond in the beginning it will only do the dynamic redirect to non-www Aug 26, 2016 at 12:24
  • @Sylwit aren't i correct? Aug 26, 2016 at 13:52

3 Answers 3

1

I haven't paid attention to your question please try this, I've tested it, put this in exact same manner with [L,R] flag

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]
RewriteRule page/(.*)/ page.php?page=$1
RewriteRule page/(.*) page.php?page=$1
4
  • same problem, when i enter example.com/test/page/id it redirects to www.example.com/page/id while i want it to redirect to www.example.com/test/page/id Aug 26, 2016 at 14:22
  • first you have to decide whether you want to it to apply on test directory or root directory i can edit it for test directory because for test it will require three matching groups. Aug 26, 2016 at 17:22
  • applying on the test directory sounds ok, what should i change? thanks in advance Aug 26, 2016 at 21:02
  • made some changed to your code and it worked, thanks Aug 26, 2016 at 22:50
0

To redirect non www requests to www, add the following rule bellow RewriteEngine line in your htaccess

RewriteCond %{HTTP_HOST} !^www
RewriteRule ^ http://www%{HTTP_HOST}%{REQUEST_URI} [L,R]
1
  • it sends me to www.example.com/test/page/id?page=id Aug 26, 2016 at 12:22
0

Try this

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule page/(.*)/ page.php?page=$1
RewriteRule page/(.*) page.php?page=$1
4
  • sends me to: www.example.com/page/id (as I said on the question the page.php is placed on www.example.com/test) Aug 26, 2016 at 14:04
  • Sorry but looks like no one understand what you want. Can you edit your question to be more explicit please.
    – Sylwit
    Aug 26, 2016 at 14:07
  • Everyone who gave an answer was looking for the same solution, so if everyone failed, maybe it's because question in unclear
    – Sylwit
    Aug 26, 2016 at 14:21
  • if you think so.. have a nice day Aug 26, 2016 at 14:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.