.htaccess : Rewriting initial output back into subdomain - .htaccess

I have the following htaccess.
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www|m)\.DOMAIN\.com$
RewriteCond %{HTTP_HOST} ^([^\.]+).DOMAIN.com$
RewriteRule ^(.*)$ http://DOMAIN.com/index.php?id=%1 [NC,QSA,L,R=301]
The problem is that although it works I need it to still use the subdomain as the URL. So basically now I'm trying to rewrite DOMAIN.com/index.php?id=%1 back into SUDOMAIN.DOMAIN.com so the user sees the subdomain they requested on the address bar and not the http://domain.com/index.php?id=user
I'ver tried all sorts of things but always end up with some sort of infinite loop. Please help!
I already have a wildcard subdomain set up. In conjunction with the htaccess shown above.
The submain doesn't stick, it rewrites itself into the long string shown above. But what im trying to do is get the url ro remain subdomain.domain.com unfortunately the link you posted does peretty much the same as what I have posted above, doesn't really seem to be able to get the sub-domain to stick.
Any ideas?

Not sure if you figured it out yet, but your last line (I believe) should be this:
RewriteRule ^(.*)$ index.php?id=%1 [NC,QSA,L,R=301]
If that does not work try this:
RewriteRule (.*) index.php?id=%1 [NC,QSA,L,R=301]
No expert but I have subdomains working from some other code I found. Hope it helps.

You need to change your DNS settings before. (A record)
There is already an answer for that question.

Related

.htaccess redirect with multi-subdomain

I'm currently using this rule to redirect traffic (except one url) to a new url.
It works fine when using a non www url like "subdomain.olddomain.com" but when accessing it with www like "www.subdomain.olddomain.com", I get a redirection error. This is the rule i use:
RewriteCond %{REQUEST_URI} !^/exception($|/)
RewriteRule ^(.*)$ https://www.newdomain.com/ [R=301,L]
What am I missing here? I'm not an expert in any way with htaccess… Thank you!
PS: I already searched for answeres but couldn't find any matching my subdomain issue.
Seems like I had to change some domain settings, the rule above works as expected.

htaccess is working but does not replace the url

I'm trying to modify the subdomain name in the URL to make it look nicer. My current URL look something like:
www.mystore.com/productInfo.php?cPath=11_11&productID=222
So, I want to make it nicer by Rewrite in .htaccess in main with this code:
RewriteEngine On
RewriteRule ^productInfo/([0-9_-]+)/([0-9_-]+) productInfo.php?cPath=$1&productID=$2 [NC,L]
When I run and test it on the URL by typing www.mystore.com/productInfo/11_11/222 in the URL it works well. However, when this page is redirected by a different page or is 'refreshed' with a self redirecting a href= link(in which the link is written in php by the previous programmer), the above old link is still visible in the URL instead of the new one.
I am still a beginner while I suspect that I might need to change something in the cPanel/Apache(I think) for this but currently, I am still do not have access to the cPanel control. Is there anything that I might have missed to write in the .htaccess file or I really do need the cPanel control or any other reasons?
Any help is appreciated. Sorry that I could not find similar questions on this.
You can use the following :
RewriteEngine On
RewriteBase /
#redirect /productInfo.php?cPath=foo&productID=bar to /productInfo/foo/bar
RewriteCond %{THE_REQUEST} /productInfo\.php\?cPath=([0-9_-]+)&productID=([0-9_-]) [NC]
RewriteRule ^ productInfo/%1/%2? [L,R=301]
#rewrite new URL to the old one
RewriteRule ^productInfo/([0-9_-]+)/([0-9_-]+) productInfo.php?cPath=$1&productID=$2 [NC,L]

mod_rewrite - change top level domain

im trying to find out an issue with the following mod_rewrite Rule:
On my Account on "Strato" (Hoster) the Rule is working, however it is not working on "1und1" (another Hoster). Is there anything wrong with that rule?
# Rewrite Rule
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^.*original-domain.de [NC]
RewriteRule /* http://new-subdomain.anotherdomain.de%{REQUEST_URI} [P]
# End Rewrite Rule
Is there perhaps another or even better way to get the redirect done?
The Goal is that a User who is visiting http://www.original-domain.de (with or without www.) should see the content of http://new-subdomain.anotherdomain.de but the original-domain Adress still stays in the Adressbar of the Browser. In other words, the User won't even recognize that he got redirected.
And, another Question: What stands the [P] for after the Rewrite Rule? If i change it to [L] the redirect works but with the new-subdomain.anotherdomain.de Adress beeing displayed in the Browser? Couldn't find a Website that describes that properly.
Thanks for your help in advance!

Unable to add www prefix to my website

I am trying to add www prefix to my domain. My domain is developigner.com.
I have used this code in .htaccess file but still am not able to get the desired result.
Here's my .htaccess file
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{http_host} ^developigner.com [nc]
RewriteRule ^(.*)$ http://www.developigner.com/$1 [R=301,nc,L]
The problem is that if I type developigner.com it gets redirected to www.your_domain.com and if I type www.developigner.com it works perfectly.
I want your_domain to be replaced by developigner
Any help would be greatly appreciated.
Thanks
It works for me as expected: developigner.com redirects to www.developigner.com.
I notice you only registered the domain yesterday. It's possible this problem is actually caused by delays in the global propagation of your domain name.

Redirect dynamic subdomain to same subdomain with subpage. How?

I'm a little stuck in here. I need to get some help with this subdomain-situation.
I need to redirect http://dynamicsubdomain.example.com/ to
http://dynamicsubdomain.example.com/account/welcome.html.
How do I do this? I tried several things but all without result. The main problem is that I can't fetch the entered dynamic subdomain from the %{HTTP_POST} string from mod_rewrite.
Another issue would be that it's creating and endless loop. So it only needs to redirect on these conditions, not when there's a URL like http://dynamicsubdomain.example.com/test/page.html. Because else it will create and endless loop. It's just for the starting page from the website.
I hope y'all can help me out, it's one of the last but important things from my project.
Thanks in advance!
There are several options on the URL redirection wiki page. For example, how about dropping an index.php in the root that redirects to the destination?
header("Location: http://dynamicsubdomain.example.com/account/welcome.html");
Why does the domain matter so much if you are staying on the same domain, and just redirecting to a different path?
The UseCanonical setting in Apache may have an effect on this, but it is defaulted to on, which preserves the host and port specified in the request.
RewriteRule ^/$ /account/welcome.html [R,L]
Hey guys, thanks for your support/help but I found the solution myself. Quicker than I thought :)
This is what does the trick, I hope I can help someone with this:
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{HTTP_HOST} ^([A-Za-z0-9]+).example\.com [NC]
RewriteRule ^ http://%1.example.com/account/welcome.html [L]
#gahooa: I need to do this because my mainpage example.com is just a sort of landing-page with no special things. The extra part of the URL "account/welcome.html" is for showing a page related to the "subdomains"-account (gahooa.example.com for example will show your profile page). In my app I catch up the subdomain by a preg_match so it knows witch account it has to load. I hope I'm clear :) But thanks anyway!
This is my first time i'm using Stackoverflow but it actually works great! Quick replies from experts, great work! I definitely will come back :)
If you really want to use HTTP_HOST:
RewriteRule ^$ http://%{HTTP_HOST}/account/welcome.html [L,R]
But like gahooa already said you don’t specify an absolute URL if you stay with the same host. The URL path will suffice:
RewriteRule ^$ /account/welcome.html [L,R]

Resources