htaccess mod_rewrite carry folder names over - .htaccess

I'm trying to redirect a URL with whatever folder is on the end to a new URL and I can't get the rewritecond figured out.
I'm trying to take URL like this:
http://www.example1.com/feature/this-folder-name/whatever1/whatever2
to
http://www.example2.com/whatever1/whatever2
The folder whatever1 can change as can whatever2.
I've tried:
RewriteCond %{HTTP_HOST} ^www\.example1\.com/feature/this-folder-name$
RewriteRule ^(.*)$ https://www.example2.com/$?&%{QUERY_STRING}
And
RewriteCond %{HTTP_HOST} ^www\.example1\.com/feature/this-folder-name$ [NC]
RewriteRule ^(.*)$ https://www.example2.com/$1 [R=301,L]

There are a number of issues here, so I took the liberty to clean things up a bit...
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example1\.com/$
RewriteRule ^/?feature/this-folder-name/(.*)$ https://www.example2.com/$1 [R=301,END,QSA]
In case you want to be more precise and really only redirect if two separate folders are specified as you explained this might point you into the right direction:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example1\.com/$
RewriteRule ^/?feature/this-folder-name/([^/]+)/([^/]+)/? https://www.example2.com/$1/$2/ [R=301,END,QSA]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
These rules will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

<VirtualHost *:80>
ServerName example1.com
redirect / http://example2.com
</VirtualHost>
<VirtualHost*:80>
ServerName example2.com
</VirtualHost>
Then let example2.com do what it does in its own virtual host settings. The assumption here is example2 knows where it’s own files are, independent of example1.

Related

Redirect htaccess rule giving 404

I have .htaccess file for seo friendly url. My standard url is;
index.php?p=user
And I can access this url by typing;
www.mydomain.com/user
Everything is fine until here. What I also want to do is to crate a seo url for the following url;
index.php?p=user&username=john
and the seo url should be as follows;
www.mydoamin.com/user/john
I have tried the following and it keeps throwing 404 error.
RewriteRule ^user/([^/]*)$ /index.php?p=user&username=$1 [L]
Can anybody tell me what is wrong here?
Here is the current .htaccess code
RewriteEngine On
RewriteRule ^(.+)$ index.php?p=$1 [L,QSA]
RewriteRule ^user/([^/]*)$ /index.php?p=user&username=$1 [L]
Considering the current configuration you added to the question the issue might be that you need to reverse the order of those directives:
RewriteEngine On
RewriteRule ^user/([^/]*)$ /index.php?p=user&username=$1 [L]
RewriteRule ^(.+)$ index.php?p=$1 [L,QSA]
The reason is that the directives get processed from top to bottom. That means that you need to implement more specific rules, so exceptions earlier, so further up in the file. Because the pattern ^(.+)$ will match all requests.
There are some additional modifications I would suggest. But you will have to test that, since I have only a very limited insight into your setup:
RewriteEngine On
RewriteRule ^/?user/([^/]+)/?$ /index.php?p=user&username=$1 [END]
RewriteRule ^/?([^/]+)/?$ /index.php?p=$1 [END,QSA]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Change URL ending with Query String

I've been using a set of redirect rules for a while that have been working perfectly.
I've recently expanded a part of my website and need to change the ending of a certain URL.
Old URL: /clan/{query-string}/tracking/war
New URL: /clan/{query-string}/tracking/warlog
I've changed my .htaccess file so the new URL works, but I need the old URL to redirect to the new one.
Currently, this is how I'm redirecting in .htaccess:
# Rewrite Clan Tracking-Warlog URL
RewriteEngine On
RewriteCond %{THE_REQUEST} /clanTracking_main.php\?name=([^\s]+) [NC]
RewriteRule ^.+$ /clan/%1/tracking/warlog [L,R]
RewriteRule ^clan/([^/]+)/tracking/warlog clanTracking_main.php?name=$1 [L]
It works perfectly but I just need help with the redirection.
Thanks for your help in advance!
I'd say the first rule below is what you ask...
I also made some other modifications which appeared to make sense to me...
# Rewrite Clan Tracking-Warlog URL
RewriteEngine On
# redirect old to new
RewriteRule ^/?clan/([^/]+)/tracking/war$ /clan/$1/tracking/warlog [R=301]
# pick name from get argument and redirect
RewriteCond %{QUERY_STRING} (?:^|&)name=([^\s]+)(?:&) [NC]
RewriteRule ^/?clanTracking_main\.php$ /clan/%1/tracking/warlog [R=301]
# rewrite to php
RewriteRule ^/?clan/([^/]+)/tracking/warlog$ clanTracking_main.php?name=$1 [END]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

how to htaccess redirect from index.php to / only in specific folder?

I want to create a redirect in my htaccess file. From URL with index.php to page without index.php, but only for one specific folder "buy-new-cars".
For example:
from example.com/buy-new-cars/index.php to example.com/buy-new-cars/
I try to add those lines.But this didn't work.
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(buy-new-cars/.+)index\.php$ /$1 [L,R=302,NC,NE]
Your issue is the RewriteCond you use which does not make sense.
Here is a version with some additional modifications:
RewriteEngine on
RewriteRule ^/?buy-new-cars/(.+)/index\.php$ /buy-new-cars/$1 [R=302,NC]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

redirect domain to sub directory using .htaccess

My server has following directory in the web directory
/mydomain/site/
/mydomain/site/project1/
/mydomain/site/project2/
I want to point domain http://mydoman.com to site directory /mydomain/site/ and access project directories using http://mydoman.com/project1/ and http://mydoman.com/project2/
I tried following code. When I type http://mydoman.com/project1/ in the browser, it is working fine but the problem is when i type http://mydoman.com/project1 (without "/" in the end of url) the url changes to http://mydoman.com/mydomain/site/project1/
RewriteEngine On
RewriteCond %{HTTP_HOST} mydomain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mydomain/site/.*$
RewriteRule ^(.*)$ /mydomain/site/$1 [L]
what I need is when I type http://mydoman.com/project1 url should not change to http://mydoman.com/mydomain/site/project1/
also
this url should not work http://mydoman.com/mydomain/site/project1/
Not sure why you don't want to use separate host names for separate projects. That would save a lot of hassle. Like https://example.com/... and https://project1.example.com/....
But anyway, this probablyis what you are looking for:
RewriteEngine on
RewriteRule ^/?mydomain/site/(.*)$ /$1 [R=301,QSA]
RewriteRule ^/?site/(.*)$ /$1 [R=301,QSA]
RewriteRule ^/?(.*)$ /mydomain/site/$1 [END]
You also need to take care that your application logic uses clean, relatvie references and not absolute paths like /site/... or /mydomain/site/... or even full URLs like https://example.com/mydomain/site/.... But that has nothing to do with rewriting. You need to solve that directly in your application logic.
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Redirect addon domain to webapge htaccess

Not quite sure if this is possible. So I have domain2.com and it maps to domain.com.
On the site I have a page called /domain-2-landing
When I hit domain2.com I want that to redirect to domain2.com/domain-2-landing or even better yet mask domain.com and serve the content of /domain-2-landing
Is this possible via .htaccess?
Your question is a bit vague, since you write about two separate things. Here are two approaches that hopefully will point you into the right direction:
To redirect any request to "domain2.com" to that "landing page" this probably is what you are looking for:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain2\.com$
RewriteCond %{REQUEST_URI} !^/domain-2-landing$
RewriteRule ^ /domain-2-landing [R=301]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
To deliver the content of that page as a response to requests to "domain.com" there are two diffferent situations:
If the domains are served from separate http servers you can use the proxy feature integrated into the rewriting module if the proxy module is installed:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule ^ https://domain2.com/domain-2-landing [P,END]
If both domains are served from a single http server you can do something similar as above, if both hosts share the same DOCUMENT_ROOT:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule ^ /domain-2-landing [END]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
These rules will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Resources