1

I have a problem with subdomains I used to translate my website. With the 4 first lines of code, I've no problem, the sub var is passed fine (http://en.mywebsite.com/ gives http://mywebsite.com?sub=en). The 5th line is used to manage ads, once I click on the link like http://en.mywebsite.com/blue-chair-Vha6J.html the page isn't loaded and I stay on the home page. It should gives me something like http://mywebsite.com?sub=en&menu=ad&ad=Vha6J. Instead of this, it gives me http://mywebsite.com?sub=en.

RewriteBase /
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP_HOST} ^([a-z]+)\.mywebsite\.com$ [NC]
RewriteRule ^(.*) ?sub=%1 [NC,L]

RewriteRule ^([-a-z0-9]+)-([A-Za-z0-9]+)\.html$ ?sub=%1&menu=ad&ad=$2 [L]

[EDIT 1]

Ok, here is a bigger htaccess extract, included the modifications I made with the first answer, and I still have problems :

RewriteBase /

RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} !&menu=.*
RewriteCond %{HTTP_HOST} ^([a-z]+)\.mywebsite\.com$ [NC]
RewriteRule ?sub=%1 [NC,L]

RewriteCond %{HTTP_HOST} ^([a-z]+)\.mywebsite\.com$ [NC]

RewriteRule ^cart$ ?sub=%1&menu=cart [L]
RewriteRule ^login$ ?sub=%1&menu=login [L]

########### AD
RewriteRule ^([-a-z0-9]+)-([A-Za-z0-9]+)\.html$ ?sub=%1&menu=ad&ad=$2 [L]
RewriteRule ^([-a-z0-9]+)-([A-Za-z0-9]+)\.html\/([A-Za-z0-9]+)$ ?sub=%1&menu=ad&ad=$2&secret=$3 [L]

########### ADS
RewriteRule ^all\/([-A-Za-z0-9]+)$ ?sub=%1&menu=ads&section_name=$1 [L]
RewriteRule ^all\/([-A-Za-z0-9]+)\/([-A-Za-z0-9]+)$ ?sub=%1&menu=ads&section_name=$1&category_name=$2 [L]
RewriteRule ^all\/([-A-Za-z0-9]+)\/([-A-Za-z0-9]+)\/([-A-Za-z0-9]+)$ ?sub=%1&menu=ads&section_name=$1&category_name=$2&subCategory_name=$3 [L]

########### SHOPS
RewriteRule ^shops$ ?sub=%1&menu=shops [L]

########### SHOP
RewriteRule ^([-a-z0-9]+)$ ?sub=%1&menu=shop&shop=$1 [L]
RewriteRule ^([-a-z0-9]+)\/([-a-z0-9]+)$ ?sub=%1&menu=shop&shop=$1&category_name=$2 [L]

When I'm on the home page https://en.mywebsite.com/, the sub var "en" is passed fine. If I go on https://en.mywebsite.com/login for instance, the page is loaded, but the sub var "en" isn't passed.

Any idea ? Thanks.

Xavier C.
  • 1,809
  • 4
  • 24
  • 40

1 Answers1

0

the problem is that the url is Rewritten twice , the first time /sample-vg.html is rewritten correctly to ?sub=en&menu=ad&ad=fg, then in the second run the new url is been rewritten again to ?sub=en (L flag just stop the current run, not the entire process, take a look here)

there is also a problem with the second RewriteRule(undefined %1),

the following code should work as expected:

RewriteBase /
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} !&ad=.*
RewriteCond %{HTTP_HOST} ^([a-z]+)\.mywebsite\.com$ [NC]
RewriteRule ^(.*) ?sub=%1 [NC,L]

RewriteCond %{HTTP_HOST} ^([a-z]+)\.mywebsite\.com$ [NC]
RewriteRule ^([-a-z0-9]+)-([A-Za-z0-9]+)\.html$ ?sub=%1&menu=ad&ad=$2 [L]
Community
  • 1
  • 1
Amine Hajyoussef
  • 4,381
  • 3
  • 22
  • 26
  • Thank you Amine, it works fine. But, now my problem is I have many different rules like the one `RewriteRule ^([-a-z0-9]+)-([A-Za-z0-9]+)\.html$ ?sub=%1&menu=ad&ad=$2 [L]` So, do I have to add on the top, for each rule, something like `RewriteCond %{QUERY_STRING} !&ad=.*` where "ad" changes? Is there a solution to manage all **%{QUERY_STRING}** in one line ? – Xavier C. Jul 01 '13 at 06:26
  • I've updated my first post, including the modifications, if you want to look at it. – Xavier C. Jul 01 '13 at 08:56