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
Related
How do I redirect from example.com/file.php to example.com/file.php?id=1 with .htaccess?
I want that .htaccess adds automatically the ?id=1 to this specific link, but not to other .php files, only this specific file.php.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)id=1(&|$) [NC]
RewriteRule ^file\.php$ $0?id=1 [L,NC,QSA,R=302]
References:
Apache mod_rewrite Introduction
Apache mod_rewrite Technical Details
Apache mod_rewrite In-Depth Details
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 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]
I am looking to migrate from old domain to new domain.
I have my old domain olddomain.com and new domain newdomain.com pointing to same ip address for now.
I have Apache server inplace to handle requests.
How do I 301 redirect all my
olddomain.com/*
&
www.olddomain.com/*
to
newdomain.com/*
Can I get exact regex or configuration that I need to add in htaccess.
My newdomain.com and olddomain.com both are being serverd by same apache from same IP so "/" redirect might lead to cycles? And so was looking for effecient way
I tried
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost$ [OR]
# RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ http://comp16/$1 [R=301,L]
</IfModule>
And even tried adding in virtual host
RedirectMatch (.*)\.jpg$ http://comp17$1.jpg
But it does not redirect site when i hit localhost in browser to my computer name i.e comp16
In the configuration (VirtualHost) for each of your olddomain.com host try this:
Redirect permanent / http://newdomain.com/
Apache documentation for Redirect. This is the preferred way when everything should be redirected. If you must use mode_rewrite/htaccess there are plenty of questions around this on SO and one of them is:
How do I 301 redirect one domain to the other if the first has a folder path
EDIT
Recommendation from Apache regarding simple redirects:
mod_alias provides the Redirect and RedirectMatch directives, which provide a means to
redirect one URL to another. This kind of simple redirection of one URL, or a class of
URLs, to somewhere else, should be accomplished using these directives rather than
RewriteRule. RedirectMatch allows you to include a regular expression in your
redirection criteria, providing many of the benefits of using RewriteRule.
I also recommend to use an If statement as you can use it also in a multisite server. Just type:
<If "%{HTTP_HOST} == 'old.example.com'">
Redirect "/" "https://new.example.com/"
</If>
Write the below code in to your .htaccess and it will redirect all your old domain request to new domain.
RewriteEngine on
RewriteBase /
RewriteRule (.*) http://newdomain.com/$1 [R=301,L]
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).