Direct subdirectory to subdomain with htaccess - .htaccess

I want to redirect this: https://example.com/sub-dir/page
to this: https://subdomain.example.com/page
We use prestashop for our main site. We have some static content in subdictory that we've since moved to a subdomain. Navigating form example.com/sub-dir will direct to sub.example.com with no changes to the htaccess.
The problem is the pages won't redirect. We get a 404 errors. with example.com/sub-dir/page
I've tried this but it doesn't work.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/sub-dir
RewriteRule ^(.*)$ https://subdomain.example/$1 [R=301,L]
I've also tried this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^sub-dir/(.*)$ https://subdomain.example/$1 [L,R=301]
Also this:
RedirectMatch 301 ^/sub-dir/$ https://subdomain.example/

Try this on top of your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule . - [E=HTTP_AUTHORIZATION:%1]
RewriteRule . - [E=REWRITEBASE:/]
RewriteRule ^sub\-dir$ sub/ [L]
RewriteRule ^sub\-dir/(.*)$ https://sub-domain.example/$1 [R=301,L]

Related

Redirect all subdomains except www to main domain

I want to redirect all subdomains except www to https://www.example.com in .htaccess, how can I do this? All subdomains can be accessed.
I only want to redirect the subdomains, not inner pages on the main domain.
Examples: (Should redirect)
hello.domain.com
test.example.domain.com
www.hello.sub.domain.com
Examples of where not to redirect:
www.domain.com/hello-world
www.domain.com
Current .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain.dk$
RewriteRule ^(.*) https://www.domain.dk/$1 [QSA,L,R=301]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
This should get you started. Anything apart from www.mydomain.com will be redirected to www.example.com.
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteRule ^(.*) https://www.example.com/$1 [QSA,L,R=301]
Edit:
Remember that it's the .htaccess in your subdomain folder, or _wildcard_domain.com that you will have to create or edit for this to work.
Try this in your .htaccess:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

301 redirection from index.php only on main page

I need to redirect /index.php to the main site without index but only on the main page.
I use this code in htaccess
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
But the problem is that in Admin panel there is routing that needs index.php for pages to work properly. And this rule is deleting index.php everytime it appears on any page.
I tried adding line like this
RewriteCond %{REQUEST_URI} !^/AdminPanel/index\.php.*$
before RewriteRule but it doesn't change anything.
Hope you will help.
Remove .* from your regex:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/index\.php
RewriteRule ^index\.php$ / [R=301,L]

Rewrite rule to work consistently for subdomain and subdirectory

I have a nice rule for rewriting the url to /meetings, but the rule doesn't rewrite the url properly when visited from a subdirectory.
EDIT: My .htaccess file is in the subdirectory /blog
When I use this rule:...
RewriteRule ^meetings$ /?page_id=2809 [L]
When visited from subdomain(blog.example.com/meetings): blog.example.com/meetings
When visited from subdirectory(example.com/blog/meetings): 404 error
When I use this rule:...
RewriteRule ^meetings$ http://blog.example.com/?page_id=2809 [L]
When visited from subdomain(blog.example.com/meetings): blog.example.com/meetings
When visited from subdirectory(example.com/blog/meetings): blog.example.com/?page_id=2809
Is there a way to get the rule to work consistently for the subdomain and the subdirectory?
You may try this in one .htaccess file in /blog directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /blog
RewriteCond %{HTTP_HOST} blog\.example\.com [NC]
RewriteCond %{QUERY_STRING} !page_id= [NC]
RewriteRule ^meetings /?page_id=2809 [L,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{QUERY_STRING} !page_id= [NC]
RewriteRule ^meetings /blog/?page_id=2809 [L,NC]
For permanent redirection, replace [L,NC] with [R=301,L,NC].

.htaccess rewrite to redirect root subdirectory to another subdirectory

Trying to get
www.example.com/alpha
to
www.example.com/beta
I am trying this but its not working
RewriteEngine on
rewritecond %{http_host} example.com/alpha [nc]
rewriterule ^(/)?$ beta [L]
Try this:
RewriteEngine on
RewriteCond %{http_host} example.com\alpha$
RewriteRule \beta$ - [L]
For More Information: Click Here
http://wiki.apache.org/httpd/RewriteCond

how to modify .htaccess file to always redirect to www

I would like to modify my .htaccess file so that when someone comes into my site without typing www the site always redirects them to the www version. For example, if my url is www.abc.com and they just type abc.com, I want to redirect them to abc.com.
Here is my current htaccess file:
<IfModule mod_rewrite.c>
RewriteBase /
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Normally I know how to do the redirect, but im having issues since it already has those few lines in there.
I use the code below. It can be used for any domain name. You just need to enter it in your .htaccess file.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
(edited to have all the code in the same block)
Add something like this immediately after RewriteEngine on:
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*) http://www.example.com/$1 [R=301]
There are two methods available
1) use mod_alias apache module
Redirect permanent /something http://yourwebsite.com/something
2) Add the following entry in your .htaccess / http.conf / yourwebsite.conf in the webserver configuarion directory
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourwebsite.com
RewriteRule ^(.*)$ http://www.yourwebsite.com$1 [R=permanent,L]
If you want redirect example.com to www.example.com you can try below code
RewriteEngine on
RewriteCond %{HTTP_HOST} !www.example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]

Resources