How to rewrite with wildcards in htaccess? - .htaccess

How can I rewrite this:
www.mydomain.com/directoryname/(wildcard)
to this:
www.myotherdomain.com/directoryname/(wildcard)
I have many subdirectories and files on the "myotherdomain.com domain name, and I need those to be accessible via the mydomain.com wildcard. I assume I will need to use htaccess to achieve this, but have not bee able to figure out the correct syntax.

Try adding these rules to the htaccess file in the document root of the www.mydomain.com site:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^directoryname/(.*)$ http://www.myotherdomain.com/directoryname/$1 [L,R]
That will redirect the browser, which means the address in the URL changes. If you don't want it to change, then you need to have mod_proxy loaded, and change the R to a P. If mod_proxy isn't loaded, then the P flag won't work.

Related

Same IP subdomains rewrite

I have the following situation:
Both 'some.example.com' and 'other.example.com' are pointing to the same IP.
But I have to change 'some' to 'other', like if I access 'some.example.com' it would "redirect" me to 'other.example.com'. It's more like a rewrite than redirect.
I looked around google looking for some htaccess solutions, but no success.
Thanks!
Assuming you have the DNS for both subdomains sorted out (e.g. you don't get a server-not-found error), this is how you do it:
First enable mod_rewrite on your server. Make sure mod_rewrite is in the right folder, and uncomment the corresponding entry in the config file. If you are in a shared host, this might already be done. Make sure Options allows FollowSymLinks. Again, if you are on a shared host, this might already be done, since you can't access the main config file in that case.
If some.example.com points to it's own directory add the following to a .htaccess file in the directory that points to:
RewriteEngine on
RewriteCond %{HTTPS}s (off|on(s))
RewriteRule http%2://other.example.com%{REQUEST_URI} [R,L]
If both subdomains point to the same directory, add the following to a .htaccess in that directory:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^some\.example\.com
RewriteCond %{HTTPS}s (off|on(s))
RewriteRule http%2://other.example.com%{REQUEST_URI} [R,L]
If you want more of a 'rewrite' instead of a 'redirect', replace the R flag with the P flag. Please note that this will cause some search engines to penalize your site in the search results, something you generally don't want.

How to htaccess a file to be displayed from other domain?

I have a domain a.com with a file called style.css. And i have cookieless domain b.com. Both are on the same server. Now what i would like to acomplish is to write a htaccess rule that states:
if user wants to take b.com/style.css display him the file from the a.com/style.css folder.
I want to acomplish it with htaccess because i don't want to involve PHP to do this (performance issue) and i would like it to work transparent for the browser. What i mean the browser asks for file from b domain and gets it from b domain.
How to acomplish it with htaccess and does it impact performance?
You can try this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?b\.com$ [NC]
RewriteRule \.(?:jpe?g|gif|bmp|png|tiff|css|js)$ http://a.com%{REQUEST_URI} [R=301,L,NC]
This does change change the URL to a.com/style.css.
If you don't want external redirection then only way out is to mod_proxy which may not be enabled already on your host. If you enable mod_proxy on b.com then you can try:
RewriteCond %{HTTP_HOST} ^(www\.)?b\.com$ [NC]
RewriteRule \.(?:jpe?g|gif|bmp|png|tiff|css|js)$ http://a.com%{REQUEST_URI} [P,L,NC]

htaccess Rewriterule for Multilanguage Website

I have a Multilanguage-Website with 2 domains like www.domain.com and www.domain.de
Now I want to define some RewriteRules like:
RewriteRule ^category/(.*)$ http://www.domain.de/$1 [R=301,L]
But I dont know how I can change it to work for both domains at once. Changing that target just without the domain doesnt work.
Example:
RewriteRule ^category-[0-9]{1,4}/(.*) http://www.domain.de/category/$1 [R=301, L]
This would redirect the user to the .de-domain. Doesnt matter which domain he was on preview pages. So visitors from domain.com would be suddenly on domain.de on some specific pages with this rule.
I want this rule to work for both domains and dont know how to write the target path.
I found a solution.
RewriteBase /
and RewriteRule without domain

How do I rewrite the url?

Could someone tell me how to rewrite this URL. I have looked at a lot of questions on stackoverflow but they seem to be missing my answer.
RewriteEngine On
That is what I have... its a bit poor.
I need to rewrite url's if they do not point to a directory.
I need to do this...
any.domain.com/pages/some-page-slug/login
To be rewritten to the correct url of...
any.domain.com/pages/login.php?page=32
Does anyone have any ideas on how this can be achieved?
1) Rewriting product.php?id=12 to product-12.html
It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1
2) Rewriting product.php?id=12 to product/ipod-nano/12.html
SEO expert always suggest to display the main keyword in the URL. In the following URL rewriting technique you can display the name of the product in URL.
RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2
3) Redirecting non www URL to www URL
If you type yahoo.com in browser it will be redirected to www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection?? Please check the post about SEO friendly redirect (301) redirect in php and .htaccess.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^optimaxwebsolutions\.com$
RewriteRule (.*) http://www.optimaxwebsolutions.com/$1 [R=301,L]
4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
5) Redirecting the domain to a new subfolder of inside public_html.
Suppose the you’ve redeveloped your site and all the new development reside inside the “new” folder of inside root folder.Then the new development of the website can be accessed like “test.com/new”. Now moving these files to the root folder can be a hectic process so you can create the following code inside the .htaccess file and place it under the root folder of the website. In result, www.test.com point out to the files inside “new” folder.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1
TO do this you need to write a front controller.
See here, here, here, and here.
Alternatively in Apache you can rewrite this
any.domain.com/pages/32/login
or this:
any.domain.com/32/login
or even this:
any.domain.com/some-slug/32/login
to this:
any.domain.com/pages/login.php?page=32
One way or another to do this with only apache you need to supply the page id in some fashion. Keep in mind even with format any.domain.com/some-slug/32/login the content of the slug is irrelevant and won't necessarily link to the correct page. Which I imagine is undesirable and bad for SEO.
Another alternative is using RewriteMap. But this will be tricky and require reloading apache configurations whenever a page/slug is created/edit.
I understand that pages and login are static in this case and some-page-slug is changing. And you always want to redirect to static page /pages/login.php?page=32
So this is how to do it:
1) Rewrite
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32
or 2) Redirect Pernament
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=301,L]
or 3) Redirect Temporary
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=302,L]
Here is great article about htaccess trics
http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/

htaccess redirect for subdomains -> similarly-named subdirectories?

I'm restructuring a web site with a great deal of content currently parked at URLs that look like this.
http://string.domain.com/year/month/dd/string-pulled-from-title
For various reasons, I'd like to park all new content at URLs that looks like this
http://www.domain.com/blogs/string/year/month/dd/string-pulled-from-title
I'd like to make the change for future content, but don't want all the old stuff to go 404.
I believe a 301 redirect rule in my htaccess will do the trick, sending all referred traffic coming in through old links to the new formats.
But what should this rule look like? I've read a few tutorials but haven't found this exact case in any examples.
Note, I don't want to do this for all subdomains, only for about 10 specific ones. So if someone could help me figure out one of these, then I can copy paste it 10 times in my htaccess for each subdomain and be set.
Drop this into the .htaccess file of the old site (adjusting the domain to your actual one):
RewriteEngine On
RewriteRule ^(.*)$ http://example.com/blogs/string/$1 [R=301]
This will grab this part of the URL at the old site:
year/month/dd/string-pulled-from-title
and redirect it to the new site under the new location:
blogs/string/year/month/dd/string-pulled-from-title
Alternatively, if you want something a little more variable like, without having to custom fix each .htaccess, drop this in the file for each subdomain instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]
If you're redirecting to the same domain, and it includes the www, adjust the rewrite rules to the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]
Note the second RewriteCond which checks to make sure that the URL requested does not include the leading www, which may lead to an endless redirect if the destination URL itself includes www and would try and redirect that subdomain as well.
%1 grabs the first capture group from the line above.
$1 references the first capture group on the same line.

Resources