I run a Wordpress Multisite that uses domain mapping. All domains point to the same Wordpress installation on, let's say, admin.com. They share the same .htaccess file.
However, with my basic .htaccess knowledge, I can't manage to make up a redirect that results in the following:
site1.com/admin > site1.admin.com/app
site2.net/admin > site2.admin.com/app
site3.fr/admin > site3.admin.com/app
And so on.
Can this be done using regex in a redirect? Any help is much appreciated!
Certainly this is possible. And actually it is not exactly complex, you can implement one rule per case:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site1\.com$
RewriteRule ^/?admin$ https://site1.admin.com/app [R=301]
RewriteCond %{HTTP_HOST} ^site2\.net$
RewriteRule ^/?admin$ https://site2.admin.com/app [R=301]
RewriteCond %{HTTP_HOST} ^site3\.fr$
RewriteRule ^/?admin https://site3.admin.com/app [R=301]
This rule set 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 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.
I do not see a way to implement a single rule handling all cases, since there is no mapping function defined for that. Most likely you will need to adapt the site names to your real cases (doubt you actually use the domain admin.com...), but above rules should give you the basic direction from where you can work.
There is nothing complex here, all of this is documented and shown with easy and realistic examples in the apache documentation. You really should start looking into that if you have questions about rewriting.
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).
Related
I'm trying to do a weird redirect in htaccess but i don't know how to do.
I have old urls like:
1. www.oldsite.com/known-old-folder/unknown-subfolder-1
2. www.oldsite.com/known-old-folder/unknown-subfolder-2/unknown-second-subfolder-1
i have to redirect respectively to new urls like:
1. www.newsite.com/known-new-folder/unknown-subfolder-1
2. www.newsite.com/known-new-folder/unknown-subfolder-2
I don't want to consider "unknown-second-subfolder-1" if present.
How can i do in htaccess?
I don't know what to do in my RewriteRule to ignore the "unknown-second-subfolder-1" if present.
Thanks a lot
You may use this redirect rule in known-old-folder/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/known-old-folder/(.+) [NC]
RewriteRule ^ http://www.newsite.com/known-new-folder/%1 [L,NE,R=301]
Sounds pretty straight forward. You need to implement a redirection along the lines of the following rule inside your old site's configuration:
RewriteEngine on
RewriteRule ^/?(?:[^/]+)/([^/]+)/? https://new.example.com/new/$1 [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...
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 do I redirect IP to actual domain? For example:
123.45.678.901 to www.example.com
and all subfolders and subpages like
123.45.678.901/all-pages to www.example.com/all-pages
Also, my SearchConsole also recognizes "top linking sites" for the same IP but in this manner:
IP 123.45.678 ---------------------------- 38,132 LINKS
IP 123.45.678.901 ------------------------- 3,617 LINKS
So I probably should redirect 123.45.678 as well. Is that possible and how?
One more thing - IP opens in HTTP and domain opens in HTTPS protocol.
Your comment to your question indicates that this is much easier than the description in the question itself indicates... The following should point you into the right direction, though you may have to tweak it for your situation. It odes not implement any specific address (which might change) but instead redirects every requested host name (so also ip addresses) that do not match your desired host name. In case you want to add exceptions you can di that using RewriteCond directives, for example in case your http server serves multiple host names / domains.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [QSA,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...
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).
Here is the solution to this kind of problem
In the .htaccess file put next code:
RewriteBase /
RewriteCond %{HTTP_HOST} ^123\.45\.678\.901$
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
So if you have a URL wit IP like
123.45.678.901/all/other/pages/
after implementing this code all pages are going to be redirected to
www.example.com/all/other/pages/
I hope i could help anyone because this is one of very important factors which may ruin your SEO.
I have the following in my htaccess file which forwards all domains pointing to this location to somedomain.uk which is working:
RewriteCond %{HTTP_HOST} !^www\.somedomain\.uk
RewriteRule ^(.*)$ http://www.somedomain.uk$1 [R=301,L]
Is there a way to pass the redirecting domain to somedomain.uk as a parameter.
e.g. going to example.com would then redirect to https://www.somedomain.uk/?url=example.com
Thanks
Sure that is possible:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.somedomain\.uk$
RewriteRule ^ http://www.somedomain.uk%{REQUEST_URI}?url=%{HTTP_HOST} [R=301]
I would however suggest to use another parameter name, since a host name is not a "url".
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 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 dynamic 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).
My CMS is running into a loop often, because impossible URLs are generated. Those URLs have the format
www.xy.com/STRING1/something/STRING2/something
or
www.xy.com/something/STRING1/somethingother/anything/STRING2/something
I want to deny or block all URLs that contain STRING1 and STRING2 as segments.
Thanks for any idea!
There are many issues with the accepted answer above, so I will post a correction...
RewriteEngine on
RewriteRule ^/?STRING1/.*/STRING2/ https://example.com/ [R=301]
Note: the L flag does not make any sense when you implement an external redirection. Also the matching is not really precise in the accepted answer...
In case you want to return an explicit http status (like 403) you can also do that:
RewriteEngine on
RewriteRule ^/?STRING1/.*/STRING2/ - [R=403]
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).
You can do it with this rewriting:
RewriteRule STRING1\/.*\/STRING2.* https://www.xy.com/ [R=301,L]
I just converted a website over to a new implementation. The old site had URLs that ended in .html; now I'm using the "normal" WordPress way in which they look like a directory. How do I redirect:
https://www.hhl.com/articlename.html?a=b
...to https://www.hhl.com/articlename/?a=b
Your question is a bit vague, but I assume that roughly is what you are looking for:
RewriteEngine on
RewriteRule ^/?articlename\.html$ /articlename/ [R=301,QSA]
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).
UPDATE:
In the comments you explained that the "articlename" portion of the URL is dynamic, that was not mentioned in the original question. Here is an updated version taking that into account:
RewriteEngine on
RewriteCond %{REQUEST_URL} !-f
RewriteRule ^/?([a-z0-9_-])\.html$ /$1/ [R=301,NC,QSA]