RewriteRule doesn't return valid result - .htaccess

My first attempt was this:
RewriteCond %{HTTP_HOST} .*\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/assets/$1 [NC,L]
My second attempt:
RewriteCond %{QUERY_STRING} ^(.*)$ [NC]
RewriteRule ^(.*)$ http://example.com/assets/$1 [NC,L]
But none of them works. This .htaccess file is on my every subdomain. My goal is when you enter URL like http://foo.example.com/vendor/css/jquery-ui.css or http://bar.example.com/vendor/css/jquery-ui.css both should return same content as http://example.com/assets/vendor/css/jquery-ui.css.
I have tested it on this website htaccess Tester. On that website it returns valid URL, but when I upload it to the FTP, trying any subdomain will redirects me to http://example.com/assets/jquery-ui.css. It should't proceed just a file name but whole path and also without redirect, it has to "look alike".

Related

htaccess not redirecting correctly

I am trying to redirect to a new domain, but it isn't retaining the complete url on the redirect, it only goes to the homepage. Here is the line in my htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*\.old-domain\.com
RewriteRule ^(.*)$ http://www.new-domain.com/$1 [R=permanent,L]
For example, if I go to www.old-domain.com/contact-us/request-a-demo/, it should go to www.new-domain.com/contact-us/request-a-demo/
Instead, anything I type after the first "/" is redirecting to the homepage for www.new-domain.com
What am I missing, please help
Maybe you could try this:
RewriteEngine On
RewriteCond %{SERVER_NAME} ^(.+\.)?old-domain\.com
RewriteRule ^/?(.*)$ http://www.new-domain.com/$1 [R=permanent,L]

Wild card URL rewrite of subdomain to subdirectory

I am in a situation where an user can create his blog in a subdomain.
Users will create blogs and enter the address he wants like say,
abcd.domain.com Then my php code creates a directory called abcd.
To view the blog user will type in abcd.domain.com in his browser and I want a .htaccess code which will rewrite the url and open the files inside the domain.com/abcd
But for the user the url in the browser should stay abcd.domain.com
Currently I am trying this code
RewriteCond %{HTTP_HOST} ^test\.domain\.com$
RewriteCond %{REQUEST_URI} !^test/
RewriteRule ^(.*)$ /test/$1 [L,QSA]
But this gives me 404 even though I have a file test.html inside the test folder and trying to view that page.
Also in this situation I will have to manually make change to the .htaccess file for URL rewrite. What I want to know is if it is possible to have a wild card subdomain redirect to the respective directory.
You can use:
RewriteCond %{HTTP_HOST} ^test\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^(.*)$ /test/$1 [L,QSA]
REQUEST_URI with leading /.
With wild card subdomain:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/%1/
RewriteRule ^(.*)$ /%1/$1 [L,QSA]
Note that it takes more than a rewrite rule to have wildcard subdomains. Just fyi.
You need to have created a wildcard DNS record for subdomains and also tell apache to use any subdomain request by having a ServerAlias of *.domain.com in the apache config.
Then try your rule this way and see if it works for you.
RewriteCond %{HTTP_HOST} ^((?!www).+)\.domain\.com$ [NC]
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule ^(.*)$ /%1/$1 [L,QSA]

Conditional URL rewrite?

I am trying to setup an htaccess redirect for the following situation.
If an app hits a URL containing /cms as the first segment http://originaldomain.com/cms I want to redirect to a secure domain https://differentdomain.com/cms.
If a URL hits https://differentdomain.com on any URL other then act, URL, system, or is a not a post then I want to redirect the user to http://originaldomain.com/requestedurl.
act is OK:
https://differentdomain.com/act?fiudsbsdfn=sfds
URL is OK:
https://differentdomain.com/url
system is OK:
https://differentdomain.com/system
POST methods are OK to any domain just redirect GET methods
Is this possible within an htaccess file?
Assuming both domains are on the same root folder and host:
RewriteCond %{HTTP_HOST} ^originaldomain\.com$
RewriteCond %{REQUEST_URI} ^/cms
RewriteRule ^(.*)$ https://differentdomain.com/$1 [L,R=302]
If they are not on the same root and folder:
RewriteCond %{REQUEST_URI} ^/cms
RewriteRule ^(.*)$ https://differentdomain.com/$1 [L,R=302]
Now the 2nd part if the url is not a act, url, system or post:
RewriteCond %{THE_REQUEST} !^[A-Z]{3,}\s/(act\?(.*)|url|system)$ [NC]
RewriteCond %{THE_REQUEST} !^POST [NC]
RewriteRule ^(.*)$ http://originaldomain.com/$1 [L,R=302]
Basically this should work, if it does after you test change to 302 to 301 if needed.

Why does the following htacces directive not work as expected?

I have the following redirects:
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^id=409$
RewriteRule ^(.*)$ http://www.domain.eu/index.php\?id=4 [R=301,L]
RewriteCond %{REQUEST_URI} foo_Bar\.pdf$ [NC]
RewriteRule ^(.*)$ http://www.domain.eu/index.php\?id=4 [R=301,L]
The first one works fine.
But the second one is not directing, when I open this url: domain.eu/fileadmin/images/foo_Bar.pdf (showing an 404 instead, thats why I want to redirect).
I've also tried to add a ^(.*) before the filename, but it doesnt work either. I've even tried to enter full url as the request uri, no luck. What am I missing?
Thanks
It will redirect www.yourdomain.com/index.php?id=409 to http://www.domain.eu/index.php?id=4
And the second will redirect if your site address ends with .foo_Bar.pdf redirects to http://www.domain.eu/index.php?id=4
You need to change the second one to:
RewriteCond %{REQUEST_URI} foo_Bar\.pdf$ [NC]
RewriteRule ^(.*)$ http://www.domain.eu/index.php\?id=4 [R=301,L]

Redirect only sub pages using htaccess

I have a website (WordPress multisite) for which I've moved a bunch of content from the route domain (http://domain[dot]com) to a sub domain (http://sub.domain[dot]com). Now I need to direct users to all the pages of the route site (http://domain[dot]com/page) to their new location (http://sub.domain[dot]com/page). But... and here's the bit I'm really struggling with... I need to omit the route url from this re-write as there is another 'geo-redirect' in place that I need to not affect. What I need to do therefore is redirect ONLY those sub page and NOT the parent/main domain.
Here's (a recent iteration) of what I'm working with:
# ignore the home page, not working :(
RewriteCond %{HTTP_HOST} !^(.*)\.routetogreatness\.com$ [NC]
# redirect all the sub pages, works
RewriteCond %{HTTP_HOST} ^routetogreatness.com [NC]
RewriteRule ^(.*)$ http://global.routetogreatness.com/$1 [L,R=301]
Any help will be very gratefully received.
I think what you are searching for is a condition that checks if the file that is requested is not a filename. That's what RewriteCond %{REQUEST_FILENAME} !-f. It is true if %{REQUEST_FILENAME} (I believe an absolute path to a file on the server, based on the request), is not a file. (Please note: I haven't tested this code as I don't have access to a server at this location, but I think it should work.)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [L,R=301]
An other solution would be to only rewrite the url if the requested url contains a slash. It would redirect domain.com/folder/index.php, but not domain.com/index.php.
RewriteCond %{REQUEST_URI} ^/([^/]+)/(.+)$
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [L,R=301]
Okay, so the above solution didn't quite work out for me as it turned out that while this worked beautifully on sub/child page, it would skip over parent pages as well as the home page (like domain/news for example. I've eventually run with this:
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [L,R=301]
This just skips over the home page or root URL and redirects everything else, even 404s.

Resources