mod_rewrite subdomain (WITH a path) to URL - apache

Struggling with this and have already done about 30+ min of Googling.
I would like to redirect:
sub.domain.com/path1 to https://anotherdomain.com/path3
sub.domain.com/path2 to https://anotherdomain.com/path4
This code below does not work but demos my approach/thinking so far
#CLIENT 1
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mockups.domain.com$
RewriteCond %{REQUEST_URI} ^/client1/
RewriteRule (.*) https://xd.adobe.com/view/xxxxxxxxxxxx/ [L,R]
</IfModule>
# CLIENT 2
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mockups.domain.com$
RewriteCond %{REQUEST_URI} ^/client2/
RewriteRule (.*) https://xd.adobe.com/view/xxxxxxxxxxxx/ [L,R]
</IfModule>
Can anyone sort me out on this?

With your shown samples only, could you please try following.
Make sure you keep these rules on TOP of your .htaccess file and keep other rules below these.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mockups\.domain\.com$ [NC]
RewriteRule ^client1 https://xd.adobe.com/view/your_new_path_here [L,R=302,NE]
RewriteCond %{HTTP_HOST} ^mockups\.domain\.com$ [NC]
RewriteRule ^client2 https://xd.adobe.com/view/your_2nd_path_here [L,R=302,NE]
</IfModule>
Also please do clear your browser cache before testing your URLs.

Related

htaccess redirect if one query param matches

I'm trying to redirect from /somedir/index.php?action=something&id=x to /index.php?action=something&id=x
Only if action = something. id is dynamic.
Most recently I've tried this with no luck. What is wrong with this?
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} (^|&)action=something($|&)
RewriteCond %{QUERY_STRING} (^|&)id=($|&)
RewriteRule ^somedir/index\.php$ /index.php?action?something&id=%2$ [NC,R]
</IfModule>
Note: somedir has an index.php and this rule in its htaccess. Will that result in conflicts?
RewriteRule ^.*$ index.php [NC,L]
Try this rule instead of your rule in .htaccess
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} (^|&)action=something($|&)
RewriteCond %{QUERY_STRING} (^|&)id=x($|&)
RewriteRule ^somedir/index\.php$ /index.php?action=something&id=x [L,R=301]
</IfModule>
The result will not conflict.
You can use:
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} (?:^|&)(action|id)=([^&]+)&(?:.*&)?(action|id)=([^&]+)(?:$|&) [NC]
RewriteRule ^somedir/index\.php$ /index.php?%1=%2&%3=%4 [NC,L,R=301]
</IfModule>

htaccess redirect specific IP

I want to redirect some IPs to a error.php . Ofc I don't want redirects from error.php to itself, so I excluded that. Bottom line, i don't figure it out where i'm wrong.
In this one i'm redirecting to google. But still failing.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} ^xxx\.xxx\.xxx\.xxx [OR]
RewriteCond %{REMOTE_HOST} ^yyy\.yyy\.yyy\.yyy
RewriteCond %{SCRIPT_FILENAME} !\/error\.php [NC]
RewriteRule .* http://google.com [R=302,L]
</IfModule>
Have you tried just using a negative lookahead?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} ^xxx\.xxx\.xxx\.xxx [OR]
RewriteCond %{REMOTE_HOST} ^yyy\.yyy\.yyy\.yyy
RewriteRule ^(?!error\.php) http://google.com [R=302,L]
</IfModule>

Laravel htaccess issue

I'm trying to set a site live. The site works perfectly fine on a live dev server which we've been using to show the site to the client. Here is the htaccess from the live dev (works fine):
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
#RewriteCond %{HTTPS} !=on
#RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
#RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Rewrite to 'public' folder
RewriteCond %{HTTP_HOST} ^livedev.domain.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) public/$1 [L]
</IfModule>
AuthType Basic
AuthName "dev16"
AuthUserFile "/home/site/.htpasswds/public_html/passwd"
require valid-user
And here's the .htaccess from the live site:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
#RewriteCond %{HTTPS} !=on
#RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
#RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Rewrite to 'public' folder
RewriteCond %{HTTP_HOST} ^livesite.co.uk$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) public/$1 [L]
</IfModule>
The 2 are identical except for the HTTP_HOST and the removal of the authentication.
It gives me a generic "Internal Server Error".
I have tried deleting the contents of .htaccess which just gives me page not found so the issue definitely lies in .htaccess.
A total .htaccess virgin, what steps can I take to find the cause of the issue?
Thanks
(It's Laravel 3.2.13)
Use at same level of /public
This way first redirect to public
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]
And inside /public
then, you handle index.php
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I had exactly same setup as RamiroRS answer, but still got Internal Server Error. My problem was in file permissions. Folders should be 0755 and files 0644.

Redirecting from subdomain without changing URL

I want to redirect a subdomain with the .htaccess file without changing the URL. But can't figure it out.
ex. i want to turn
http://foo.domain.com
into
http://www.domain.com/users/foo/dashboard.php
I generated a script to do this, but the URL doesn't stay on http://foo.domain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
RewriteRule ^ http://www.domain.com/users/%1/dashboard.php [L]
Thanks for the help!
What you're htaccess it is doing is simply saying "if you find this, do a full redirect to that url"
Do this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
RewriteRule ^ dashboard.php?domain=%1 [L, QSA]
And it should work (moving to wherever dashboard is in your directory structure on-server, relatively)
Try this (assuming that all the subdomains point to the root directory) :
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^.*$ users/%1/dashboard.php [L]
Proxy flag [P] will do the trick:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.org$ [NC]
RewriteRule http://example.org/users/%1/dashboard.php [P,NC,QSA]
</IfModule>

Symfony2 - Rewrite url to add www prefixwith existing .htacess file

Symfony2 core configuration comes up with the following .htacess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
I understand that this will erase the app.php from the url in the browser.
I would like to add the following rewrite rule to add www. to my WebSite everytime a user enter the domain name without the www. :
RewriteCond %{HTTP_HOST} ^valutao\.com$ [NC]
RewriteRule ^(.*)$ http://www.valutao.com/$1 [L,R=301]
So now i have the following .htacess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
RewriteCond %{HTTP_HOST} ^valutao\.com$ [NC]
RewriteRule ^(.*)$ http://www.valutao.com/$1 [L,R=301]
</IfModule>
My issue is that app.php will show up again. It seems that my rule just hides the base configuration. Here is an example of what is displayed in the url when I enter valutao.com : http://www.valutao.com/app.php
Does someone know how to fix that. Unfortunately I have just basic knowledge with the rewrite rules.
Thanks for help
You simply need to change the order (www rewrite should go first).
I'd also make it less dependent on the domain name:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>