I am looking for help to set rewrite rules in .htaccess.
I have multiple websites powered by single Modx installation. For redirecting browser to right context I am using gateway plugin which works good eben without .htaccess modification. The problem has arised when I decided to make one of the hosted websites multilingual. I installed Babel plugin and everything got messed up.
lets say the structure is:
domain1.com
domain2.com
domain3.com/en/
domain3.com/de/
I need to set rules in .htaccess to detect browser language if requested http host is domain3.com and according to detected language add en or de to path.
You can try this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)domain3\.com$ [NC]
RewriteCond %{HTTP:Accept-Language} ^([a-z]{2}) [NC]
RewriteRule !^[a-z]{2}/ /%1%{REQUEST_URI} [L,R]
Related
I have requests like https://example.net/files/public/file.html which I would like to redirect to https://example.com/domain/public/file.html via htaccess.
In theory I would have to write an if condition and then remove the files part from the URI and then redirect to the new domain. But in practice my code doesn't work.
Has anyone a working example for this type of scenario?
Cheers
In site root directory of example.net you can use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.net$ [NC]
RewriteRule ^files/(.*)$ http://example.com/domain/$1 [L,NC,NE,R=301]
References:
Apache mod_rewrite Introduction
Apache mod_rewrite Technical Details
Apache mod_rewrite In-Depth Details
I have a Wordpress install on domain1.com/blog but ideally I would like it to display as sub.domain2.com/blog.
These domains are on separate servers. Ideally I would install WP on same server as domain2 but this isn't possible right now.
I've been able to get sub.domain2.com/blog to display the WP homepage but is it possible for htaccess and apache to rewrite the urls on domain1.com to display as sub.domain2.com?
Other than simple url rewrites I have limited experience with htaccess but I suspect for security reasons this isn't possible.
You can of course, redirect your requests on domain1.com to sub.domain2.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(blog/.*)$ http://sub.domain2.com/$1 [R=301,L]
Put the above in a htaccess file for the domain1.com.
I have a domain and a different server with a Symfony2 website. I created a proxy redirection from the domain name to the server. That's working now.
But I have a problem. When I want to login with the FOSUserBundle, I see my IP address of the server (http://xxx.xxx.xxx.xxx/login). I want to show my domain name instead.
Do I have to create an Apache2 RewriteRule or do I have to configure Symfony2? I have tried different RewriteRules, but it's not working.
Obviously Apache2. In Symfony you can configure only routes for controllers..
Something like that
RewriteCond %{HTTP_HOST} ^121\.12\.12\.123
RewriteRule (.*) http://www. mydomain.com/$1 [R=301,L]
I find it out, you have to add the following lines after Symfony2 RewriteRules. I placed my own rules before, and that caused the redirect loop.
RewriteCond %{HTTP_HOST} ^xxx\.xxx\.xxx\.xxx
RewriteRule (.*) http://url.nl/$1 [L]
Here is more information about rewriting urls in Symfony2:
http://www.symfonylab.com/tricks-with-symfony-htaccess/
I've a domain on which I'm running two different web applications at different ports.
A Java web application on http://example:8888/foo
A PHP website on http://example:8080/bar
The requirement is, if an user tries to access the root of http://example:8888 or http://example:8080, then the user should be redirected to http://example:8888/foo.
How can I achieve this requirement?
mod_rewrite can achieve this easily, you will be using 301 redirects
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^/$ http://example.com:8888/foo [R=301,L]
Make sure this is added in the vhost config section of both sites.
We have two domains, let's call them first.com and second.com
We have a directory in second.com called reports, where all our PDFs are located, but we would like to these same PDFs accessible from first.com as well.
Can we redirect let's say first.com/reports/84839049.pdf to second.com/reports/84839049.pdf using htaccess?
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^domain\.com
RewriteRule (.*) http://domain1.com/$1 [R=301, L]
Yes.
redirect /requested/url http://second.com/result/url
http://httpd.apache.org/docs/1.3/mod/mod_alias.html#redirect
You may want to consider using mod_rewrite though, unless you asked for an .htaccess configuration specifically because you have no access to the server configuration and mod_rewrite is disabled or not loaded.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://webdesign.about.com/od/mod_rewrite/qt/site_redirects.htm
You'll need some grasp of regex for mod_rewrite, but it can make configuration of the redirects a lot faster than having to add a redirect for every file on your site(s).