Htaccess subdomain + multilingual website - .htaccess

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.

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]

Related

Convert display language to subfolder with htaccess

For a website I have locally, I need to set the display language as if it were a subfolder, using an htaccess file.
For example, if I choose to display the site in German, instead of displaying it like this => local_ip/test/index.php?lang=de, I would like it to be displayed like this => local_ip/test/de/ .
Also, if I type in the url without setting the language, I would like htaccess to automatically redirect to the German language, from so => local_ip/test/tour/mountain/ to so => local_ip/test/de/tour/mountain .
My current htaccess file is structured as follows:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{QUERY_STRING} ^t=(.+?)
RewriteRule ^city/(.+?)/?$ city.php?url=$1&t=%1 [NC,L]
RewriteCond %{QUERY_STRING} ^p=([0-9]+)
RewriteRule ^city/(.+?)/?$ city.php?url=$1&p=%1 [NC,L]
RewriteCond %{QUERY_STRING} ^t=(.+?)&p=([0-9]+)
RewriteRule ^city/(.+?)/?$ city.php?url=$1&t=%1&p=%2 [NC,L]
RewriteCond %{QUERY_STRING} ^c=true
RewriteRule ^city/(.+?)/?$ city.php?url=$1&c=true [NC,L]
RewriteRule ^city/(.+?)$ city.php?url=$1 [NC,L]
RewriteRule ^tour/(.+?)$ tour.php?url=$1 [NC,L]
SOLVED
After several attempts and after reading several support requests posted in this community, I found a way to make the system recognise all the other rules as well. All I had to do was put them before the last rule provided by the user who supported me. Here is the final code.
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !/de/ [NC]
RewriteRule (.*) /test/de/$1 [R,L]
RewriteRule ^de/$ /test/index.php?lang=de [L]
RewriteCond %{QUERY_STRING} ^t=(.+?)
RewriteRule city/(.+?)/?$ city.php?url=$1&t=%1&lang=de [NC,L]
RewriteCond %{QUERY_STRING} ^p=([0-9]+)
RewriteRule city/(.+?)/?$ city.php?url=$1&p=%1&lang=de [NC,L]
RewriteCond %{QUERY_STRING} ^t=(.+?)&p=([0-9]+)
RewriteRule city/(.+?)/?$ city.php?url=$1&t=%1&p=%2&lang=de [NC,L]
RewriteCond %{QUERY_STRING} ^c=true
RewriteRule city/(.+?)/?$ city.php?url=$1&c=true&lang=de [NC,L]
RewriteRule city/(.+?)$ city.php?url=$1&lang=de [NC,L]
RewriteRule tour/(.+?)$ tour.php?url=$1&lang=de [NC,L]
RewriteRule ^de/(.*)$ /test/$1 [L]
You can use the following :
RewriteEngine on
#redirect URLs to include /de
#RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/de/ [NC]
RewriteRule (.*) /test/de/$1 [R,L]
# /test/index.php?lang=de to /test/de
RewriteRule ^de/$ /test/index.php?lang=de [END]
RewriteRule ^de/(.+)$ /test/$1 [END]

dot is not working in htaccess as defined in htaccess rules

I don't have much knowledge in .htaccess and I am struggling with regex concept in .htaccess.
I want same url concept as on facebook for user profile but dash(-), underscore(_), dot(.) this I want in url also currently my url rule is as follow:
RewriteRule ^([a-z,\_,A-Z,\_,\-,0-9.][!pagenotfound|home|abuse/*|user/*|start/*|login/*|account/*|message/*|find/*|cms/*|accountactivation/*|register/*]*)/?$ memberspersonalinfo.php?members=$1 [QSA]
This pattern have some error
but when in my browser domain.com/username is comming it's working fine but when url will be domain.com/test-test or test_test or test.test it redirects to `PAGE NOT FOUND PAGE
and in my .htaccess rule defined to follow that rule like abuse/* have dynamic id wherever I have used star (*) they are referencing dynamic value. When I will do rule like
RewriteRule ^([a-z,\_,A-Z,\_,\-,0-9.]*)/?$ memberspersonalinfo.php?members=$1 [QSA]
It's working fine but when I moveto home page then it calls memberspersonalinfo.php page it self.
I am trying to explain my Question again with htaccess script
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
DirectoryIndex index.html index.php
ErrorDocument 404 /pagenotfound
ErrorDocument 403 /pagenotfound
ErrorDocument 401 /slapme/pagenotfound
ErrorDocument 500 /pagenotfound
#index
RewriteRule ^index.html$ index.php [L]
RewriteRule ^index$ index.php [L]
RewriteRule ^home$ index.php [L]
#nopage
RewriteRule ^pagenotfound$ nopage.php [L]
#Regitration
RewriteRule ^register/createaccount$ userregistration.php [L]
#Regitration Confirmation
RewriteRule ^register/accountconfirmation$ registrationconfirm.php [L]
#Activation Regitration
RewriteRule ^accountactivation/([a-z,\_,A-Z,\_,\-,0-9]*)/([a-z,\_,A-Z,=,\_,\-,0-9]*)$ registrationactivation.php?UsercontactID=$1&AccountId=$2 [L]
#after login
RewriteRule ^user/main$ after_login.php [L]
#Payment Detail
RewriteRule ^user/myaccount$ useraccount.php [L]
#Logout
RewriteRule ^user/logout$ logoutuser.php [L]
#creategroup
RewriteRule ^start/creategroup$ http://domain.com/start/creategroup [L,R=301]
#Forgot Password
RewriteRule ^user/forgotpassword$ http://domain.com/user/forgotpassword [L,R=301]
#Login
RewriteRule ^login/userlogin$ http://domain.com/login/userlogin [L,R=301]
#Remove account
RewriteRule ^account/remove$ removeuseraccount.php [L]
#Send an email page
RewriteRule ^message/?$ contacttouser.php [L,QSA]
#Report any site user page
RewriteRule ^abuse/([0-9]*)/$ reportsiteuser.php?reportUserID=$1 [L,QSA]
#CMS FIND FRIENDS PAGE
RewriteRule ^find/friend$ http://domain.com/find/friend [L,R=301]
#CMS TERMS OF USE PAGE
RewriteRule ^cms/howitworks$ howitworks.php [L]
#CMS CREATE OWN SLAP PAGE
RewriteRule ^cms/createownslap$ createownslap.php [L]
#CMS BRAND OR PRODUCT PAGE
RewriteRule ^cms/brandorproduct$ brandorproduct.php [L]
#CMS APPS AND FEATURES PAGE
RewriteRule ^cms/appsandfeatures$ appsandfeatures.php [L]
this my full htaccess code everthing is working fine all rules are working but when i add
#group personal profile page page
RewriteCond %{REQUEST_URI} !/abuse(.*)
RewriteCond %{REQUEST_URI} !/user(.*)
RewriteCond %{REQUEST_URI} !/start(.*)
RewriteCond %{REQUEST_URI} !/login(.*)
RewriteCond %{REQUEST_URI} !/account(.*)
RewriteCond %{REQUEST_URI} !/message(.*)
RewriteCond %{REQUEST_URI} !/find(.*)
RewriteCond %{REQUEST_URI} !/cms(.*)
RewriteCond %{REQUEST_URI} !/accountactivation(.*)
RewriteCond %{REQUEST_URI} !/register(.*)
RewriteCond %{REQUEST_URI} !/social(.*)
RewriteCond %{REQUEST_URI} !/images(.*)
RewriteCond %{REQUEST_URI} !/css(.*)
RewriteCond %{REQUEST_URI} !/js(.*)
RewriteCond %{REQUEST_URI} !/userimages(.*)
RewriteRule ^([a-z,\_,A-Z,\_,\-,0-9.]+)/?$ memberspersonalinfo.php?members=$1 [QSA,L]
it always loads memberspersonalinfo.php

.htaccess, cannot get first part of a URL in rewrite condition

I am attempting to get the following results with an .htaccess file.
URL: Behavior(rewrite to):
example.com/x/ view-test.php?page=x
example.com/x/y view-test.php?page=y&parent=x
example.com/x/y/z view-test.php?page=z&parent=y&grandparent=z
I have the following .htaccess rules, which also include some folders which needs to ignore this scheme:
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]*)/([^/]*)/([^/]+)/?$ /view-test.php?grandparent=$1&parent=$2&page=$3 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]*)/([^/]+)/?$ /view-test.php?&parent=$1&page=$2 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
#RewriteRule ([^/]+)$ /view-test.php?page=$1 [L]
however, whenever I uncomment the last two lines I get an internal server error. the parent/page and grandparent/parent/page structures work fine. How can I re-write this last rule (and the preceding rules if nessecary) to achieve the desired result for the first pattern (just a page)?
# remove slash at the end to prevent redirect loop
RewriteCond %{REQUEST_URI} ^/(OIE|admin|images|scripts|css|fonts|view-test\.php) [NC]
RewriteRule ^ - [L]
RewriteRule ([^/]*)/([^/]*)/([^/]+)/?$ /OIE/view-test.php?grandparent=$1&parent=$2&page=$3 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]*)/([^/]+)/?$ /OIE/view-test.php?&parent=$1&page=$2 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]+)/?$ /OIE/view-test.php?page=$1 [L]
Use this code instead:
RewriteCond %{REQUEST_URI} ^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ^ - [L,S=3]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ view-test.php?grandparent=$1&parent=$2&page=$3 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ view-test.php?&parent=$1&page=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ view-test.php?page=$1 [L,QSA]
Problem was that you were missing trailing slash / in in your last RewriteRule condition.
Try this :
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)$ view-test.php?page=$1
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ view-test.php?page=$1&parent=$2
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ view-test.php?page=$1&parent=$2&grandparent=$3

Why is this RewriteRule broken?

Ok folks, I'm at a loss.
RewriteCond %{HTTP_HOST} ^domain.nl$
RewriteRule ^(.*)$ poker/$1 [L]
Throws me a 500 error. If I remove the redirect and go to /poker/ manually it works. If I use this:
RewriteCond %{HTTP_HOST} ^domain.nl$
RewriteRule ^$ poker/ [L]
The front page is shown (but the css not, because obviously anything after the / is not redirected.
What could cause this (.*) to break?
Before this rule is just this to remove www:
RewriteCond %{HTTP_HOST} ^www\.([a-z-]+)\.([a-z]{2,3})
RewriteRule ^(.*)$ http://%1.%2/$1 [R=301,L]
After it is nothing that could interfere.
It was so easy.
RewriteCond %{HTTP_HOST} ^domain.nl$
RewriteRule ^(.*)$ poker/$1 [L]
Gives an infinite loop. I just had to add:
RewriteCond %{HTTP_HOST} ^domain.nl$
RewriteCond %{REQUEST_URI} !^/poker
RewriteRule ^(.*)$ poker/$1 [L]
(or as I just found out, creating an empty .htaccess with just RewriteEngine On in the /poker/ dir did the trick as well).

Domain to point to a single page

I'm trying to point a domain name to a single page, and keep the domain the same (no redirect).
So if a user types: www.domain1.com.au --> original site is shown
If a user types: www.domain2.com.au --> they are shown www.domain1.com.au/second.php, but the URL still says www.domain2.com.au.
Can this be done using .htaccess?
Snippet of current .htaccess file is:
RewriteCond %{HTTP_HOST} www\.domain2\.com\.au [NC]
RewriteRule (.*) /two [L]
RewriteCond $1 ^(one|two|three|four) [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
So basically the www.domain2.com.au needs to display the www.domain1.com.au/two page, which really is www.domain1.com.au/index.php/two
You could definitely do something like this:
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com\.au$
RewriteCond %{REQUEST_URI} !^/domain1
RewriteRule (.*) /domain1/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com\.au$
RewriteCond %{REQUEST_URI} !^/domain2
RewriteRule (.*) /domain2/$1 [L]
Your exact case would similar to this:
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com\.au$
RewriteRule (.*) /second.php [L]
Edit: Now that you've posted a snippet try this instead:
RewriteCond %{HTTP_HOST} www\.domain2\.com\.au [NC]
RewriteRule (.*) /index.php/two [L]
RewriteCond $1 ^(one|two|three|four) [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
You want to show a different site than what the URL says.
That requires proxying.
Read this: http://httpd.apache.org/docs/2.0/mod/mod_proxy.html

Resources