How can I Alias Or Rewrite the subfolder to a domain - php

I have a folder structure like this
/
/project1
I normally use it with one domain (example.com), but now I want to use it as a sepreate domain (project1.example.com) and I am using apache virtualhost.
I have succeed to setup, but some problem on the link happened.
All the hyperlink in project1 is the path included the "project1" subfolder (e.g. project1/image/pic.jpg).
I setup like this
DocumentRoot /doc_root/
Alias /project1 /doc_root/project1/
Alias / /doc_root/project1
RewriteRule /project1/(.*) /$1 [R]
The problem is when i rewrite clean url
e.g. example.com/project1/post?id=1 => project1.example.com/post/1
With
RewriteRule ^/post/(\d+)$ /project1/post.php?id=$1 [PT,NC,L,QSA]
I am getting 404 error in the ajax post in the page
Since the ajax url is pointed to /project1/process.php
And I think this request is not rewriting.
What is the proper way to setup like this? thanks!

There's several issues here:
The Alias rules don't look correct and are confusing.
The redirect rule you provided won't extract a query string parameter (i.e. it won't rewrite the path from /project1/post?id=1 to /post/1) nor does it alter the domain name (i.e. it won't change the domain to project1.example.com).
There's no need for the redirect.
I think what you want is something like:
DocumentRoot /doc_root/
RewriteCond %{HTTP_HOST} ^project1\.example\.com$
RewriteRule ^/(.*)$ /project1/$1 [NC,PT]
If you do actually want to redirect, consider:
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^/project1/(.*)$ project1.example.com/$1 [NC,R]

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
…
}

.htaccess; redirect do subdirectory without showing in URL

I have multiple domains like:
www.example.com
www.myexample.com
www.this-is-an-example.com
All domains point to the main path / on my server.
How can I point a single domain to a subdirectory on my server without showing un the URL?
I mean instead of www.myexample.com pointing to / on my server it should point to /myexample/ on my server.
So calling www.myexample.com via browser should open /myexample/index.php on my server and NOT /index.php.
And www.myexample.com/some_path/some_file.php should look for /myexample/some_path/some_file.phpnon my server but the URL should remain www.myexample.com/some_path/some_file.php and get changed to www.myexample.com/myexample/some_path/some_file.php
Does anybody know how to do this? I already tried to Google but without success maybe because I don't really know what to search for.
You can use this rule
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?myexample\.com$
RewriteRule !folder /folder%{REQUEST_URI} [L]
This will internally forward www.myexample.com or myexample.com (non-www) with requested uri to /folder
Try below rule too,
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ /myexample/$1 [L]

How do I route all subdomains to index.php using htaccess without 301 redirect

I would like to set up an htaccess file that routes requests for any subdomain to the index.php file in the /public_html/ folder. I have already configured the DNS to accept wildcard subdomains.
I plan to give users a personalised url for the site and pass the username via the subdomain, so it is important that the url remains and hence 301 redirects are out of the question.
My htaccess currently looks like this, but does not work (server not found):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteRule . /index.php?subdomain=%1 [L,QSA]
My end goal is to be able to get a url such as //joebloggs.mydomain.com/foo/bar to translate into something like //www.mydomain.com/index.php?user=joebloggs&route=/foo/bar.
What do I need to have in the htaccess file to make this work?
You can have it this way
# if request is for a subdomain (except "www")
RewriteCond %{HTTP_HOST} ^((?!www\.).+)\.mydomain\.com$ [NC]
# internally rewrite to index.php
RewriteRule ^((?!index\.php$).*)$ /index.php?subdomain=%1&route=$1 [L]
Since it seems the rule is executed when you're trying to navigate to the index.php, can't this be done with simple php logic within index.php?
I mightve misunderstood your goal, but it seems to me you could easily replace this with a GET. Your url would be ready for that approach, too.

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.

Mod rewrite base URL

I have several domains that I want to all rewrite to one domain. I don't want it to redirect because I want the URL to look like what the user has entered in. For example if they enter www.example.com I want it to load the page from www.sample.com/default.php?from=example
I have worked a little with rewriting if you have www.site.com/var1/var2 making it load www.site.com/index.php?one=var1&two=var2
Is it possible to do what I am looking for just through the .htaccess file? I tried looking around and couldn't exactly find what I was looking for
Thanks
If the sites are hosted on different servers or don't share a common document root, then you'll have to rely on mod_proxy and you can use the P rewrite rule flag. For example, these rules in an htaccess file in www.example.com's document root:
RewriteCond %{HTTP_HOST} ([^.]+)\.com$ [NC]
RewriteRule ^/?$ http://www.sample.com/default.php?from=%1 [L,P]
Will take the request http://www.example.com/ and invisibly proxy it to http://www.sample.com/default.php?from=example. The browser's URL address bar will remain http://www.example.com/.
Note that the rule only matches against the request URI /. If you want to do more, you'd have to create the correct regular expression and grouping.
If you have redirects on the sample.com site, you'll need to employ ProxyPassReverse to rewrite the redirects. Also see ProxyPassReverseCookieDomain and ProxyPassReverseCookiePath if there are cookies involved.
If you can do this in vhost or server config instead, then consider simply using ProxyPass instead of mod_rewrite. The ProxyPass directive won't work inside htaccess files.
EDIT:
Seeing as how everything is in the same document root, you won't need to proxy anything. Simply:
RewriteCond %{HTTP_HOST} !^www\.sample\.com$ [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.com$ [NC]
RewriteRule ^/?$ /default.php?from=%1 [L]
I use online generators.
Use this generator: mod-rewrite

Categories