Htaccess rules for dynamic sub domain conflicting for index file - .htaccess

Here is my htaccess file
Options +FollowSymLinks
RewriteEngine on
# For Accessing Page http://www.domain.com/news/news-details.php
RewriteCond %{HTTP_HOST} ^www.domain\.com$ [NC]
RewriteRule ^news/news-details.php$ news.php [QSA,NC,L]
# For www.domain.com it should go to the index page
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteRule ^(.*)$ index.php [NC,L]
when i type http://www.domain.com/news/news-details.php in the browser it takes me to the index.php page rather than news.php. WHY?
EDIT:
Check these rule
# For www.domain.com it should go to my-index.php page
#
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ my-index.php [NC,L]
# For Page URL http://www.domain.com/news/news-details.php
#
RewriteCond %{REQUEST_URI} ^/news/news\-details\.php [NC]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ my-news.php [NC,QSA,L]

Options +FollowSymLinks
RewriteEngine On
# For Page URL http://www.domain.com/news/news-details.php
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^news/news-details\.php$ /my-news.php [NC,QSA,L]
# For Accessing Division Page http://user1.domain.com/news/news-details.php
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com [NC]
RewriteCond %{HTTP_HOST) !^www\.
RewriteRule ^news/news-details\.php$ /my-news.php?user=%1 [QSA,NC,L]
# For Accessing Users Page
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteRule ^$ /users.php?user=%1 [L]
# For www.domain.com it should go to my-index.php page
# (but only if requested resource is not real file)
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /my-index.php [L]

You can't the name of the file in the path.
RewriteRule ^news/news-details.php$ news.php [QSA,NC,L]
here you have the file "news.php" and in the path you have "news".
try another word than news in the path or another name for the file.
for example:
RewriteRule ^news/news-details.php$ news_main.php [QSA,NC,L]

Related

Redirect all domains to use HTTPS

I have this code in my .htaccess file which does as the comments say:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
Options -Indexes
# redirect all traffic to correct domain
RewriteCond %{HTTP_HOST} ^itl\.|^(www\.)?(integratelecom|integra|integratelecommunications)\b [NC]
RewriteRule ^ https://www.example.net%{REQUEST_URI} [L,R=301,NE]
# redirect admin./ssladmin. sub domain to the correct folder
RewriteCond %{HTTP_HOST} (admin|ssladmin)\.itl\.uk\.net$ [NC]
RewriteRule !^admin/system/ admin/system%{REQUEST_URI} [L,NC]
# redirect subdomain.X to subdomain.example.net
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(?!itl\.)[^.]+\. [NC]
RewriteRule ^ https://%1.example.net%{REQUEST_URI} [L,NE,R=302]
# map subdomains to correct folder
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.example\.net$ [NC]
RewriteRule !^subdomains/ subdomains/%1%{REQUEST_URI} [L,NC]
RewriteRule ^(subdomains/|admin/|index\.php$) - [L,NC]
# stop external linking to website but allow listed domains below
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.co.uk [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(.*\.)?itl.uk.net [NC]
#RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.|admin\.|ssladmin\.)?example.net [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L]
#######################################
############## MAIN SITE ##############
#######################################
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(blog)/(post|tags|category)/([\w-]+)/?$ index.php?id=$1&type=$2&unique=$3 [QSA,NC,L]
RewriteRule ^(blog)/(archives)/([0-9]{4})/([0-9]{2})?$ index.php?id=$1&type=$2&year=$3&month=$4 [QSA,NC,L]
RewriteRule ^(support/knowledgebase)/(article|category|search)/([\w-]+)?$ index.php?id=$1&type=$2&unique=$3 [QSA,NC,L]
RewriteRule ^([a-zA-Z0-9-/_]+)/?$ index.php?id=$1 [L,QSA]
All of the above is working fine, but i need to force every domain and subdomain to use HTTPS, I have tried adding in:
RewriteCond %{HTTPS} off %HTTP_HOST [NC]
RewriteRule ^ https://%1.domain.net%{REQUEST_URI} [L,NE,R=302]
But that keeps returning an Internal Server Error
I also tried these two lines, but that adds %25 to the end of the URL
#RewriteCond %{HTTPS} off
#RewriteRule (.*) https://%{SERVER_NAME}/%$1 [R,L]
To force HTTPs you can use:
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
It basically says, if HTTPs is not equal to ON, then it will force the website to display using SSL.
Make sure you clear your cache before testing this.

Redirect www to non www with subfolder and links

Ok, so i want to redirect all www to non www link,
I want to redirect www.example.com to example.com, and i can do that too using this code.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
But when someone try to access www.example.com/example, that redirects the site to example.com,
I want a code that can redirect www.example.com/example or any subfolder to example.com/example or any subfolder.
Kinda like a wildcard entry, how to do that?
Also if possible force ssl on it too.
Thank You.
If I've understood your question correctly:
www.example.com/dir/anypage.html to example.com/dir/anypage.html (same page)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [nocase]
RewriteRule ^(.*) http://example.com/$1 [last,redirect=301]
OR Use:
RewriteCond %{HTTP_HOST} ^(www\.example\.com)?$
RewriteRule ^(.*)$ http://example.com/subfoldername/$1 [R=301,L]
-------- Alternative---------
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(example\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1 [L]
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(/)?$ /subfolder/index.php [L]
To force SSL:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

.htaccess file showing 500 Internal Server Error

I have this in my htaccess file:
RewriteCond %{HTTP_HOST} ^my\.domain\-uk\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.my\.domain\-uk\.net$
RewriteRule ^/?$ "http\:\/\/www\.domain\-uk\.net\/my\-integra" [R=301,L]
RewriteCond %{HTTP_HOST} ^status\.domain\-uk\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.status\.domain\-uk\.net$
RewriteRule ^/?$ "http\:\/\/www\.domain\-uk\.net\/service\-status" [R=301,L]
RewriteCond %{HTTP_HOST} ^dd\.domain\-uk\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.dd\.domain\-uk\.net$
RewriteRule ^/?$ "https\:\/\/sub\.domain\.co\.uk\/preauth\/0J9A7MT35N" [R=301,L]
the status. redirects but the two after are showing 500 Internal Server Error
above this i have the standard Wordpress code:
# 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
The first rules that you want in your htaccess file are the ones the redirect the browser, the order is important, you don't want to redirect the browser after wordpress's internal rewriting. So first, you can clean up your redirect rules:
RewriteCond %{HTTP_HOST} ^(www\.)?my\.domain\-uk\.net$ [NC]
RewriteRule ^/?$ http://www.domain-uk.net/my-integra [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?status\.domain\-uk\.net$ [NC]
RewriteRule ^/?$ http://www.domain-uk.net/service-status [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?dd\.domain\-uk\.net$ [NC]
RewriteRule ^/?$ https://sub.domain.co.uk/preauth/0J9A7MT35N [R=301,L]
The ? makes the grouping optional, and the NC ignored the case. So after these rules, you can put your wordpress rules.

Rewrite not working when "www" in URL

I used to have a website at www.roboticsguy.com, which I moved over to www.foxytronics.com. I want to redirect all requests from the old site to the new one. Here is my .htaccess file:
Options +FollowSymlinks
RewriteEngine on
redirect / http://www.foxytronics.com
rewritecond %{http_host} ^roboticsguy.com [nc]
rewriterule ^(.*)$ http://www.foxytronics.com/$1 [r=301,nc]
This URL works:
roboticsguy.com/test/
This one doesn't:
www.roboticsguy.com/test/
What's the problem with the rewrite and how should I fix it?
The line rewritecond %{http_host} ^roboticsguy.com [nc] means "only do the next bit if the domain name is roboticsguy.com". www.roboticsguy.com is not the same as roboticsguy.com.
rewritecond %{http_host} ^roboticsguy.com [nc]
rewriterule ^(.*)$ http://www.foxytronics.com/$1 [r=301,nc]
rewritecond %{http_host} ^www.roboticsguy.com [nc]
rewriterule ^(.*)$ http://www.foxytronics.com/$1 [r=301,nc]
or
rewritecond %{http_host} ^(www.)?roboticsguy.com [nc]
rewriterule ^(.*)$ http://www.foxytronics.com/$1 [r=301,nc]
should work.

htaccess https off condition

I have created the below condition:
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/(shop|delivery-checkout|delivery-order|delivery-offer|confirm-sms-code|show-delivery-product|auto-search-product|add-product|update-product|remove-product|destroy-cart|get-cart|cuisine|og-shop) [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/ [R=301,L]
RewriteCond $1 !^(index\.php|robots\.txt|website|shop_files|shop_list|client|favicon\.ico|style\.css|sitemap.xml)
RewriteRule ^(.*)$ ./index.php?/$1 [L]
AddCharset utf-8 .js
What I am basically trying to do is check if (https) is not present in URL.
So except URLs referred, rewrite it to https://www.
Note: I am loading those urls: domain.com/shop, domain.com/delivery-checkout, domain.com/delivery-order inside an iframe and that is why I want those excluded and secure or not depending on parent site.
What am I doing wrong?
Change your code with this:
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/(shop|delivery-checkout|delivery-order) [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

Resources