How to Chnage root path when we access from mobile .htaccess? - php

Actually I have two sources
Desktop Layout Support (/Public_html/Desktop) (example.com)
Mobile Layout support (/Public_html/Mobile) (example.com)
How can i link root path based on device access without chnage/redirect domain in .htaccess.
Actually i Have tried
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sample.in$ [NC,OR]
RewriteCond %{HTTP_HOST} ^sample.in$
RewriteCond %{REQUEST_URI} !Public_html/Desktop/
RewriteRule (.*) /Public_html/Desktop/$1 [L]
But i can't able to find device
Please help me to solve my problem.

My suggestion here that you can try to create loader.php located in /Public_html/loader.php set it as root in .htaccess and when request comes to loader.php detect device for example by method suggested here - Simplest way to detect a mobile device
after detection you have 2 options to do
1) Easy option: redirect page to example.com/desctop or to example.com/mobile
2) Hard option: include nigher /Public_html/Desktop/index.php nigher /Public_html/Mobile/index.php which for sure requires a lot additional configuration of include pats in your both projects

Related

Apache two apps one domain share language /en - Magento & Wordpress

We have Wordpress in the root / in a physical subfolder /wp and Magento in /products.
We are wanting to make the sites multi-language using sub folders e.g domain.com/en
The problem arises as magento appends the store code (language) after the url so we have
domain.com/en (wordpress)
domain.com/products/en (magento)
Naturally we would like
domain.com/en
domain.com/en/products
Now it's very easy to make it work with some rewrite rule
RewriteRule ^(.*)/products/?(.*)$ /products/$1 [L]
But still we have an issue as Magento generates the links as /products/en it's possible to start modifying where these links are generated like in
\Magento\Store\Model\Store
In the _updatePathUseStoreView function, this doesn't seem to handle all links though
In general seems like a bad solution, another idea is to use Apache mod_substitute also seems bad practice, and overhead.
Another option is to have both apps in the root and have some lookup logic to see which url belongs to which app.
Any ideas for a setup that can purely use just Nginx/Apache. That does not compromise on having unique url's or regex'ing content.
This is my .htaccess in the root
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/wp/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_URI} !^/(.*)/products
RewriteRule ^(.*)$ /wp/$1
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ wp/index.php [L]
RewriteCond %{REQUEST_URI} ^/(.*)/products
RewriteRule ^(.*)$ /products/index.php [L]
</IfModule>
The exact spec I'm trying to achieve is this.
Wordpress is installed in /wp , Magento in /products
Language codes via subfolders used on both sites to appear as /en/wordpress-page /en/products/magento-page
Attempt 1
Use base link URL entering /en/products there and keeping the base URL as /products
as the first request is forwarded I had to work the setEnv like so in the root .htaccess
RewriteCond %{REQUEST_URI} ^/(.*)/products
RewriteRule ^(.*)$ /products/index.php [E=MAGE_RUN_CODE:%1] [L]
then in /products/.htaccess
RewriteCond "%{ENV:REDIRECT_MAGE_RUN_CODE}"
RewriteRule .* - [E=MAGE_RUN_CODE:%{ENV:REDIRECT_MAGE_RUN_CODE}] [L]
I checked the code was coming through on index.php by doing
echo getenv('MAGE_RUN_CODE');
In my case the store code is "en" etc.. but the language switcher does not work it hits Magento but gets 404 even thought the store code is definitely coming through.
You only need some configuration from backoffice.
System => Configuration => General => Web => Url options
Add Store Code to Urls No
System => Configuration => General => Web => Unsecure
Base Link URL http://example.com/en/products/
System => Configuration => General => Web => Secure
Base Link URL https://example.com/en/products/
Then, add a rule in htaccess to set the correct store code:
SetEnvIf Host .*example.com/en* MAGE_RUN_CODE=en_store
SetEnvIf Host .*example.com/fr* MAGE_RUN_CODE=fr_store
What is the exact spec you're trying to achieve?
Do you have multiple pages like /products, and multiple languages like /en?
I did something similar at BXR.SU — I didn't like the way OpenGrok, my backend, was handling the URLs, so, I would automatically make my nginx fix the URLs on top of OpenGrok, seamlessly fixing the URLs presented to the user, whereas the backend would continue to use the old URLs (e.g., with the /xref/ for most pages, which I don't like, and was set to remove with nginx); this approach appears to be similar to your spec, where you want to do this on the front-end web-server without doing any modifications to the backend.
The approach is briefly described at nginx redirect loop, remove index.php from url, with the idea being that nginx has two types of redirects — internal, where the contents of the $uri variable gets changed (without any visibility to the user), and external, where a 301 Moved (or some such) response is provided to the client (and the user would then see the browser making a request with the new URL).
E.g., you may want to have something like the following:
location /en/ {
# issue an external redirect, unless we're here from an internal one
if ($request_uri ~ "^(/en)(/product)(.*)") {
return 301 $2$1$3; # external redirect
}
proxy_pass …;
}
location /products/ {
rewrite ^(/products/)(en/)(.*) $2$1$3 last; # internal redirect
…
}

PHP framework and shared hosting with ftp inside only the public_html

lately I'm trying to learn php frameworks, cause I want to implement them in my projects, implement routes and secure sessions/form handling and I'm sick of using wordpress for client's works, so I discovered Slim, and I made the example app that you can find in the project's homepage.
Now, for various reasons, I know a lot of client will end up opting for a shared hosting solution, when it's just a showcase website, so I made some research about shared hosting configuration and .htaccess file. I used altervista to make a test and see if it works (it's a free hosting service, shared), the only problem is that every tutorial I followed, assumed that when connecting through ftp you can see the doc root (public_html, htdocs and so on) but still upload out of it, instead, in this case I can only see the inside of public folder.
This had me a little bit confused, but I tried anyway, I uploaded all slim files (including public app folder and all necessary background files), made an .htaccess, and uploaded it, that's its content :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^minimaleffort.altervista.org$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.minimaleffort.altervista.org$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]
I went to the index of my website and the home screen of slim framework appears as intended (a bit edited cause I implemented bootstrap css)
Everything works, right? No, wrong, if I type a route (for example the default app route)
/{name}
It returns a 404 error page, while it should print on screen whatever name I put in the URL, am I missing something?
If you wanna check the site directly here's the url
If you check the official Slim 3 example application, its .htaccess file reads as following:
RewriteRule ^ index.php [QSA,L]
I am by no means a mod_rewrite guru, but I think you are not passing the query string to the index file. This is why the frontpage works, but not the other pages.
I think your .htaccess file should look something like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^minimaleffort.altervista.org$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.minimaleffort.altervista.org$
RewriteCond %{REQUEST_URI} !public/
RewriteRule ^(.*)$ /public/index.php [QSA,L]

htaccess mod_rewrite redirect to directory without displaying on url

I have a site called www.example.com and I have my php files in it. I store all my working files in www.example.com/site. I want to view the site in www.example.com instead, without moving my site content. What can I do?
This is currently what I am typing in .htaccess. It will redirect my site to www.example.com/site but I think the url is ugly
RewriteEngine on
RewriteBase /
RewriteRule ^(/.*|)$ /magento$1 [NC,L]
Let me see if i understood this correctly.
You have a domanin, www.example.com, and on this domain you want to display the content of a directory, www.example.com/site !?
If this is the case then you need to change the document root
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !^/site
RewriteRule ^(.*)$ /site/$1 [L]
or
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ /site/$1 [L,NC]
There are 2 things you must consider :
If I understand your request, you are accessing some PHP files using www.example.com, but what you want is to access www.example.com/site, but without the /site, right ?
So basicaly, what you're looking for is NOT rewrite, it's just pointing your domain to the good folder, which is /site, right ?
If you're using Apache2, you have to edit your apache's configuration file in /etc/apache2/site-available/default (or remplace default with the name of the virtualhost you may have created).
In this file, look for the directive DocumentRoot. It should lead to the "root" directory of your pages (the one you access typing www.example.com)
I think you just have to append /site to this DocumentRoot and then reload your apache2 with service apache2 reload
You're website www.example.com will now lead to the correct directory.
If it's still not working, you must consider looking into magento's admin, because magento is rewriting url according to the Base URL you specify inside Admin / Configuration / general / web.
You'll have to modify Base_URL in both Secure and Un-Secure sections.
Then it should work fine.
I would comment out the Rewrite bloc you're using at the moment, or maybe I didn't fully undestand what you want to achieve.

Sub Domain Name Creation in .htaccess

I have an application on SAAS basis where I will be adding clients from backend or the clients can even create their own panel by signing up
While signing up or adding the client , the client or I will be assigning the sub domain . So suppose I am assigning subdomain xyz to client1 . Then the access url will be
xyz.maindomain.com
Now , I want .htaccess code in such a way that if xyz.maindomain.com is called, the url called must be maindomain.com?client=xyz .
Please guide me how to do this.
Thanks in advance.
We could use mod_rewrite to achieve what you wanted. No server-side scripting such as PHP required.
Say, put this in .htaccess file on your root www directory (or any relevant Apache *.conf file if you wish)
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/?client=%1/$1 [L,R=301]
This will redirect foo.example.com to http://www.example.com/?client=foo
(change example.com to your domain name)
More about mod rewrite, with examples:
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
http://code.tutsplus.com/tutorials/an-in-depth-guide-to-mod_rewrite-for-apache--net-6708
Hope this help.

is it possible to have many sub-domains with the code in the root folder

i am developing a website. where in user would be asked to select their country( like USA, UK, Australia & Canada in the websites landing page.
i am planning to create a sub-domain for each country. Now my query is,
is it possible to run the website with
these 4 sub-domain with the code and
data bases in the main ROOT FOLDER
.
OR
do we need to install the code
separately in all sub-domains and
create the separate Data bases
.
Thanks in advance.
Regards,
Gourav
You can so this with a .htaccess file which redirects to the correct part of the script when they come from any sub domain.
Checkout this page as is has a similar problem and solution you have:
http://blog.gwebtools.com/apache-htaccess-subdomain-redirect-rules/
Extract:
<IfModule mod_rewrite.c>
RewriteEngine On
#redirect gwebtools.com to www.gwebtools.com
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www..*
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]*).(com|com/)
RewriteRule ^.*$ http://www.%1.%2%{REQUEST_URI} [R=301,L]
#if subdomain pt or whois and folder port-scanner redirect to pt.gwebtools.com/scanner-porta, with parameters
RewriteCond %{HTTP_HOST} ^(pt|whois)\.gwebtools\.com
RewriteRule ^port-scanner/* http://pt.gwebtools.com/scanner-porta$1 [R=301,L]
</IfModule>
Some information about .htaccess and some info so you can understand what the above code does: http://www.webweaver.nu/html-tips/web-redirection.shtml
Or you can do it in php like this tutorial:
http://php4every1.com/tutorials/multi-language-site/
You don't have to create anything seperate, but you would need to include the following in the application:
Method to recognize WHICH subdomain is loaded (en.domain.com) for english language selection, etc.
Create aliases for your domain (or just do a *.domain.com under apache for instance) to funnel the new subdomains into the proper virtual host.
Create content specific to the subdomains (you can have one DB but pull distinct data based on which domain is being used -- again you would need to check for this in your app).
You could add a conditional code that checks $_SERVER["SERVER_NAME"] variable somewhere and set the appropriate database settings based on the variable, without duplicating the code.

Categories