andrew
andrew

Reputation: 2098

htaccess redirection

Ok my project is to make an old static site into a dynamic one. There are about 40 pages in the old site.

The new format will be in 2 languages for now, with the possibility for more in the future.

There are 2 sets of urls actually:

  1. the basic ones: /index.html, /about.html, /contact.html
  2. the rest: /folder1/page1.html, /folder1/page2.html, /folder3/page3.html, etc

The client wants the 2nd language to have the same pattern as the default one:

  1. the basic ones: /language/index.html, /language/about.html, /language/contact.html
  2. the rest: /language/folder1/page1.html, /language/folder1/page2.html, /language/folder3/page3.html, etc

The basic pages i can rewrite with htaccess:

# default language    
RewriteRule ^about.html$ about.php?language= [NC,L]
#other language
RewriteRule ^([^/]+)/about.html$ about.php?language=$1 [NC,L]

As for the rest of the pages i am stuck.

In my database i have saved the existing urls and their content.

For example, for the basic language:

/folder1/page1.html will be served by dynamicpage1.php?language=&url=/folder1/page1.html

/folder2/page2.html will be served by dynamicpage1.php?language=&url=/folder2/page2.html

/folder3/page3.html will be served by dynamicpage2.php?language=&url=/folder3/page3.html

And for the other language:

/([^/]+)/folder1/page1.html will be served by dynamicpage1.php?language=$1&url=/folder1/page1.html

/([^/]+)/folder2/page2.html will be served by dynamicpage1.php?language=$1&url=/folder2/page2.html

/([^/]+)/folder3/page3.html will be served by dynamicpage2.php?language=$1&url=/folder3/page3.html

How can i construct these rules?

If i try: RewriteRule ^([^/]+)/(.*)$ subservices.php?language=$1&url=$2, it fails

Upvotes: 0

Views: 91

Answers (1)

LazyOne
LazyOne

Reputation: 165148

You better specify/list possible languages in URL Rewriting rule -- it will be MUCH MORE accurate then. This rule works fine:

RewriteRule ^((EN|FR)/)?(.*\.html)$ /subservices.php?language=$2&url=/$3 [NC,QSA,L]

or

RewriteRule ^((english|french)/)?(.*\.html)$ /subservices.php?language=$2&url=/$3 [NC,QSA,L]

Change EN|FR to whatever languages you do use.

  • /index.html will be rewritten to /subservices.php?language=&url=/index.html
  • /FR/index.html will be rewritten to /subservices.php?language=FR&url=/index.html
  • /folder1/page1.html will be rewritten to /subservices.php?language=&url=/folder1/page1.html
  • /FR/folder1/page1.html will be rewritten to /subservices.php?language=FR&url=/folder1/page1.html
  • /ZZ/folder1/page1.html will be rewritten to /subservices.php?language=&url=/ZZ/folder1/page1.html (ZZ is not recognized as acceptable language).

Upvotes: 1

Related Questions