rewrite file to subdomain - .htaccess

We used this rules
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
RewriteCond %{QUERY_STRING} ^n=(.+)$
RewriteRule ^news\.php$ http://news.mydomain.cop/news.php?n=%1 [L,R=301]
for redirect
http://mydomain.com/news.php?n=100
to
http://news.mydomain.com/news.php?n=100
but we need
http://mydomain.com/news.php?n=100
and
http://news.mydomain.com/news.php?n=100
redirect 301 to
http://news.mydomain.com/100.html

Try:
RewriteCond %{HTTP_HOST} ^(news\.)?mydomain\.com [NC]
RewriteCond %{QUERY_STRING} ^n=(.+)$
RewriteRule ^news\.php$ http://news.mydomain.com/%1.html? [L,R=301]
This will make it so if someone types: http://mydomain.com/news.php?n=100 or http://news.mydomain.com/news.php?n=100 in their browser's URL address bar, they'll get redirected to http://news.mydomain.com/100.html, thus changing the URL in the address bar.

Related

301 Redirect to new domain with some specific URLs

I saw similar topics but couldn't find a practical answer to my problem.
I'm moving my old website to a new one, and some URLs are changing.
I would like to make a generic 301 redirection to the new domain (because most paths are the same), while individually redirecting some URLs.
Here is what I have on my old website .htaccess :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old\.com$
RewriteRule (.*)$ https://new.com/$1 [R=301,L]
Redirect 301 "/custom/url/" "https://new.com/my-custom-url"
</IfModule>
But the 301 redirects to : https://new.com/custom/url instead of https://new.com/my-custom-url
Some of my URLs also have URL parameters I would like to redirect, such as :
Redirect 301 "/brand.php?name=Example" "https://new.com/Example"
Redirect 301 "/brand.php?name=Example2" "https://new.com/another/url"
which do not seem to work as well.
Thank you very much for your help.
But the 301 redirects to : https://new.com/custom/url instead of https://new.com/my-custom-url
It is because your specific redirect rule appears after generic one. Moreover you are mixing mod_rewrite rules with mod_alias rules and these are invoked at different times.
Have it like this:
RewriteEngine On
# redirect /brand.php?name=Example2 to new.com/another/Example2
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=(Example2) [NC]
RewriteRule ^brand\.php$ https://new.com/another/%1? [R=301,L,NE]
# redirect /brand.php?name=Example3 to new.com/category/Example3
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=(Example3) [NC]
RewriteRule ^brand\.php$ https://new.com/category/%1? [R=301,L,NE]
# generic redirect /brand.php?name=Example to new.com/Example2
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=([^&]+) [NC]
RewriteRule ^brand\.php$ https://new.com/%1? [R=301,L,NE]
# redirect custom URL
RewriteRule ^custom/url/ https://new.com/my-custom-url [R=301,L,NE,NC]
# redirect everything else
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteRule ^ https://new.com%{REQUEST_URI} [R=301,L]

Remove parameters from redirect

I have set up a redirect in an .htaccess file where I'm trying to remove the parameter from being added to the redirect rule. The rule I have set up is:
RewriteCond %{HTTP_HOST} ^windward\.net$ [NC]
RewriteCond %{QUERY_STRING} ^cat=1$
RewriteRule ^2013/$ https://www.windwardstudios.com/? [R=301,L]
I'm trying to redirect from 2013/?cat=1$ to https://www.windwardstudios.com/ but the redirect URL is:
https://www.windwardstudios.com/?cat=1
How do I remove the ?cat=1 from the redirected URL?
try this
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*)&?cat=[^&]+&?(.*)$ [NC]
RewriteRule ^/?(.*)$ /$1?%1%2 [R=301,L]

How to redirect https://sitename.com to https://www.sitename.com

My redirect in htaccess file successfully redirects example.com to https://www.example.com
but if I type: https://example.com in the address bar, I am not redirected.
Current code looks a bit like this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^example.local$
RewriteCond %{SERVER_NAME} !.cms.me$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Adding the www after the https:// does not work:
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This will also redirect https://example.com to https://www.example.com You need to have your rule like this.
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]
Let me know how that works out.

Dynamic subdomain with htaccess (not redirect)

Currently, I'm having an url system like this by using the htaccess:
- www.domain.com/member/username
- www.domain.com/member/username/contact
- www.domain.com/member/user/album/title-of-the-album/albumID
.. something like that
Here is my htaccess, and it worked perfectly.
RewriteRule ^member/([a-zA-Z0-9_-]+)$ member.php?username=$1
RewriteRule ^member/([a-zA-Z0-9_-]+)/contact$ contact.php?username=$1
RewriteRule ^member/([a-zA-Z0-9_-]+)/album/([a-zA-Z0-9_-]+)/([0-9]+)$ album.php?username=$1&title=$2&album_id=$3
Now I want to setup a dynamic subdomain system for the user, likes "username.domain.com"
So I decided to used the below htaccess:
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com [NC]
RewriteRule ^(.*)$ www.domain.com/member/%1 [L]
But this redirect the user to their old domain "www.domain.com/member/username" instead of "www.domain.com/member/username"
I want to keep the user stay in "username.domain.com" (no url change in the address bar).
If possible, is there any chance to keep the same structure for other new url, such as when I type:
"username.domain.com/contact" will load the content of "www.domain.com/member/username/contact" (no url change in the address bar)
"username.domain.com/album/title-of-the-album/albumID" will load the content of "www.domain.com/member/username/album/title-of-the-album/albumID" (no url change in the address bar)
Please help !!
Try:
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com [NC]
RewriteRule ^/?$ /member.php?username=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com [NC]
RewriteRule ^/?contact$ /contact.php?username=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com [NC]
RewriteRule ^/?album/([a-zA-Z0-9_-]+)/([0-9]+)$ /album.php?username=%1&title=$1&album_id=$2 [L]

redirect subdomain request to main domain using htaccess

here is what I do and works good:
#redirect subdomains to controller
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^([a-zA-Z0-9-_]*)$ blog.php?blog_uid=%1 [L,QSA]
now I want to redirect
http://example.domain.com/post-123.html
to
blog.php?blog_id=example&post_id=123
but this not works:
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^([a-zA-Z0-9-_]*)/post-([0-9]+)\.html$ blog.php?blog_uid=%1&post_id=%2 [L,QSA]
how do it? actually how redirect such subdomain request to a rewrite condition (it is not a real page).
Use this rule instead:
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteRule ^post-([0-9]+)\.html$ /blog.php?blog_id=%1&post_id=$1 [NC,L,QSA]
Try this one.
RewriteEngine On
RewriteRule ^post-([^/]*)\.html$ /blog.php?blog_id=example&post_id=$1 [L]

Resources