How to stop .htaccess ReWrite rules affecting Subdomains - .htaccess

I have this code in my .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(online-shop)/?$ $1/home [L,NC]
RewriteRule ^(my)/?$ $1/home [L,NC]
RewriteRule ^(blog)/(post|tags)/([\w-]+)/?$ index.php?id=$1&type=$2&unique=$3 [L,QSA,NC]
RewriteRule ^(blog)/(archives)/([0-9]{4})/([0-9]{2})?$ index.php?id=$1&type=$2&year=$3&month=$4 [L,QSA,NC]
RewriteRule ^([\w/-]+)/?$ index.php?id=$1 [L,QSA]
So the following URLs are rewritten:
domain.com/home to domain.com/index.php?id=home
However if i look at a subdomain (cp.domain.com) i get the following error:
The requested URL /home/user/public_html/index.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
so the subdomains are looking in the ROOT directory for the domain however they should be looking in the directory set in the control panel
As soon as i remove this line of code:
RewriteRule ^([\w/-]+)/?$ index.php?id=$1 [L,QSA]
the subdomains start working fine

You can try to restrict last rule to only work for main domain:
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w/-]+)/?$ index.php?id=$1 [L,QSA]

Related

htaccess rewrite going to 404?

for the last hours I cant seem to figure out why my .htaccess file is redirecting to a blank url. Here is my htaccess file below.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^(.*)$ /test/user-profile/%1? [R=301,L]
I am trying to rewrite www.example/test/user-profile?id=4 ->>> www.example/test/user-profile/4
It does rewrite the url, however then the page has "The requested URL was not found on this server." the htaccess file is in public_html folder, so I am trying to select a specific user inside www.exampleurl.com/test/userslist.php which would go to www.exampleurl.com/test/user-profile.php. It all works perfectly before using htaccess. www.example/test/user-profile?id=4 ->>> www.example/test/user-profile/4 but it just wont find the url or file? im a super noob folder path below -> public_html/test/userslist.php & user-profile.php
Just cant seem to figure out what to do. Yes I do have mod-rewrite on.
With your shown samples, please try following htaccess rules file.
This assumes that you are hitting link www.example/test/user-profile?id=4 in browser which is redirecting to www.example/test/user-profile/4 and is being served by index.php with parameters user-profile=user-profile in your url and id=digits in url. You can also change them as per your need.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /test/
RewriteCond %{THE_REQUEST} \s/(test/user-profile)\?id=(\d+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*/)/([*/]*)/?$ test/user-profile.php?user-profile=$1&id=$2 [QSA,L]
2nd solution: In case someone is hitting url example.com/test/user-profile/7 then try following htaccess rules.
RewriteEngine On
RewriteBase /test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*/)/([^/]*)/(\d+)/?$ test/user-profile.php?user-profile=$1&id=$2 [QSA,L]

rewrite rule on htaccess not working

Hi for some reason the htaccess file is not working properly
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?mallyear.com$
RewriteCond %{REQUEST_URI} !^/prod/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /prod/$1
RewriteCond %{HTTP_HOST} ^(www.)?mallyear.com$
RewriteRule ^(/)?$ prod/ [L]
so all the files are in this directory prod/ but when i enter this url
https://www.mallyear.com/landing
it shows "The requested URL /prod/landing was not found on this server."
why is this happening? Please help

SSL issue - too many redirects

The website in question is: sophostechnologies.com
https://sophostechnologies.com or sophostechnologies.com works fine and loads in SSL, HOWEVER, if I type www. or https://www. in front of the url name, I get too many redirects loop error.
My .htaccess file looks like this:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.sophostechnologies.com/$1 [R,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)\.(.*)$
RewriteRule ^(.+[^/])$ http://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
# Google sitemap controller
RewriteRule ^(tmp/)?sitemap.xml$ tmp/sitemap.xml [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)(\.php([0-9]*)|\.json|\.xml|\.tpl|\.phtml|\.ini|\.inc|/)$ index.php?_p=$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_p=$1 [QSA,L]
Any help is appreciated.

HT access rewrite rule stops subdomains working

I have this code in my htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(online-shop)/?$ $1/home [L,NC]
RewriteRule ^(my)/?$ $1/home [L,NC]
RewriteRule ^(blog)/(post|tags)/([\w-]+)/?$ index.php?id=$1&type=$2&unique=$3 [L,QSA,NC]
RewriteRule ^(blog)/(archives)/([0-9]{4})/([0-9]{2})?$ index.php?id=$1&type=$2&year=$3&month=$4 [L,QSA,NC]
RewriteRule ^([\w/-]+)/?$ index.php?id=$1 [L,QSA]
it stops subdomains of the domain working and shows 404 page errors because it cannot find index.php
How can i fix the sub domains?
You need to check for your domain in a condition so it's only applied to your main domain. Replace example.com with your domain.
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^([\w/-]+)/?$ index.php?id=$1 [L,QSA]

Htaccess redirect static sub-domain to domain

I created some static sub-domains for images:
www.static1.domain.com
www.static2.domain.com
Now I want to redirect files that are not images from static domains to www.domain.com, to avoid duplicate content. I have these rules in my htaccess (non-existing files are redirected to index.php silently):
#Redirect static to main
RewriteCond %{HTTP_HOST} static([0-9]+)\.
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !\.(js|css|png|jpg|jpeg|gif)$ [NC]
RewriteRule (.*) http://www.domain.com%{REQUEST_URI} [R=301,L]
#Redirect non exisitng files to index.php (silent)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
The redirect works fine. But if I enter a non-existing image e.g. http://www.static1.domain.com/test.gif, I am redirectd to http://www.domain.com/index.php.
The redirect of test.gif should be a silent redirect to index php ... what I am doing wrong?
Thanks for hints.
I'm not sure if I'm understanding you correctly. By silent redirect to you mean that "index.php" shouldn't be in the url bar or do you mean the url bar should still read "test.gif" but the page should render index.php?
You can not add multiple RewriteCond lines. Only the last one applies to RewriteRule.
So this line does not have any effect:
RewriteCond %{REQUEST_FILENAME} -f
And that is why non existing images are also being redirected.
How about:
#Redirect non exisitng files to index.php (silent)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
#Redirect static to main
RewriteCond %{REQUEST_FILENAME} !\.(js|css|png|jpg|jpeg|gif)$ [NC]
RewriteRule (.*) http://www.domain.com%{REQUEST_URI} [R=301,L]
Did it this way. Throws the default server 404 Page when http://www.static1.domain.com/test.gif is not found. Not best, but well.
RewriteCond %{HTTP_HOST} !static([0-9]+)\.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} static([0-9]+)\.
RewriteCond %{REQUEST_FILENAME} !\.(js|css|png|jpg|jpeg|gif)$ [NC]
RewriteRule (.*) http://www.domain.com%{REQUEST_URI} [R=301,L]

Resources