Website URL shows server folder structure - .htaccess

I've accidentally replaced our website's htaccess file yesterday and messed a couple of things up. I've been able to get the site back online, however, now, my site is showing the server's folder structure in the URL for all of the website's pages.
Desired:
http://gemdigitalagency.com/our-services
What is showing:
https://gemdigitalagency.com/production/gemdigitalagency/our-services/
I'd like to remove the /production/gemdigitalagency from the URL.
Here's what I have for htaccess (hosted in public_html):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^gemdigitalagency\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://gemdigitalagency.com/$1 [R,L]
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?gemdigitalagency.com$
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /production/gemdigitalagency/$1 [L]
RewriteCond %{HTTP_HOST} ^(www.)?gemdigitalagency.com$
RewriteRule ^(/)?$ production/gemdigitalagency/index.html [L]
ErrorDocument 400 /404.html
ErrorDocument 401 /404.html
ErrorDocument 403 /404.html
ErrorDocument 404 /404.html
ErrorDocument 500 /404.html
Options -Indexes
Could anyone please point me to the right direction?
Thank you!

We ended up removing a couple of things and adding an htaccess to the website's folder contents on the server.
htaccess in the public_html folder:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^gemdigitalagency\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://gemdigitalagency.com/$1 [R,L]
ErrorDocument 400 /404.html
ErrorDocument 401 /404.html
ErrorDocument 403 /404.html
ErrorDocument 404 /404.html
ErrorDocument 500 /404.html
Options -Indexes
htaccess in the website's folder:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^gemdigitalagency\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://gemdigitalagency\.com/$1 [R,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.gemdigitalagency\.com$ [NC]
RewriteRule ^(.*)$ https://gemdigitalagency\.com/$1 [R=301,L]
ErrorDocument 403 /404.html
ErrorDocument 404 /404.html
ErrorDocument 500 /404.html

This will redirect user to SSL secured web http://... onto https://
RewriteEngine On
RewriteCond %{HTTP_HOST} ^gemdigitalagency\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://gemdigitalagency.com/$1 [R,L]
This is where is your main issue
RewriteRule ^(.*)$ /production/gemdigitalagency/$1 [L]
RewriteRule ^(/)?$ production/gemdigitalagency/index.html [L]
which tells apache to rewrite everything going on host www.gemdigitalagency.com OR gemdigitalagency.com (www is optional)
RewriteCond %{HTTP_HOST} ^(www.)?gemdigitalagency.com$
to with something more in the URI
RewriteCond %{REQUEST_URI} !/$
which is not actual filename or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
rewrite to /production/gemdigitalagency/$1 where the $1 is anything behind a host (https://gemdigitalagency.com/something?get_param=1) the $1 is something?get_param=1 for example
try to change it to:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^gemdigitalagency\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://gemdigitalagency.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^(www.)?gemdigitalagency.com$
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /$1 [L]
RewriteCond %{HTTP_HOST} ^(www.)?gemdigitalagency.com$
RewriteRule ^(/)?$ /index.html [L]
ErrorDocument 400 /404.html
ErrorDocument 401 /404.html
ErrorDocument 403 /404.html
ErrorDocument 404 /404.html
ErrorDocument 500 /404.html
Options -Indexes

Related

htaccess misconfiguration 500

So basically i created a .htaccess file which removes .html extension and redirects /file.html to /file but when i write /file.html/ it causes a 500 internal server error, How can I map/file.html/ to my custom error page ? or simply redirect that also to /file.html
Here is the code :
Options -Indexes
ErrorDocument 404 /custom404.html
ErrorDocument 403 /custom404.html
ErrorDocument 500 /custom404.html
RewriteEngine on
RewriteCond %{THE_REQUEST} \.html [NC]
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
With your shown samples, could you please try following(comments have been added in rules for explanation). Please make sure to clear your browser cache before testing your URLs.
Options -Indexes
ErrorDocument 404 /custom404.html
ErrorDocument 403 /custom404.html
ErrorDocument 500 /custom404.html
RewriteEngine on
##Added additional condition to make sure this runs from external request only.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/.*\.html\s [NC]
RewriteRule ^(.*)\.html/?$ /$1 [R=301,L,NC]
##Simply rewrite from non-existing pages to html files.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^(.*)/?$ $1.html [L]

Add a new rule to .htaccess whitout overwrite https server rule

I have a problem when I try to add a new rule to my .htaccess. On my server I activated a rule which redirects http to https and it works fine but when I add a new rule to my .htaccess then https disappears.
My .htaccess
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?$1 [L]
ErrorDocument 404 https://%{HTTP_HOST}/
ErrorDocument 403 https://%{HTTP_HOST}/
</IfModule>
I need to redirect every request to index.php (mvc) and it should work together with https.
I tried deactivating the redirection from the server and to write everything into my .htaccess like this:
Options -Indexes
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?$1 [L]
ErrorDocument 404 https://%{HTTP_HOST}/
ErrorDocument 403 https://%{HTTP_HOST}/
But I get the error The page isn't redirecting properly. How to fix it? Thank you.
I've finally solved it with the help of my internet provider. This is the final code:
RewriteEngine on
RewriteBase /
# force https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP_HOST} ^www.
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# redirect to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
ErrorDocument 404 https://%{HTTP_HOST}/
ErrorDocument 403 https://%{HTTP_HOST}/

Trying to figure out this redirection in .htaccess

This is the .htaccess file for one of the sites I am working on. My apache virtualhost is set to redirect http to https automatically. I am running into a problem when I go to my http://www version. It adds index.php in between. Can you see what is wrong here?
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^.*/\.
RewriteRule ^(.*)$ - [R=404]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
ErrorDocument 404 /404.html

Htaccess - Multiple Parameters

I have a .htaccess file and I want to add more parameters to the resources page like I have now.
Now I have: /resources/<id>
But I want: /resources/<id>/<name>
This is my current .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+resources(?:\.php)?\?id=([a-z,A-Z,0-9]+) [NC]
RewriteRule ^ resources/%1? [R,L]
RewriteRule ^resources/([a-z,A-Z,0-9]+)/?$ resources.php?id=$1 [L,QSA]
RewriteCond %{THE_REQUEST} \s/+profile(?:\.php)?\?id=([a-z,A-Z,0-9]+) [NC]
RewriteRule ^ profile/%1? [R,L]
RewriteRule ^profile/([a-z,A-Z,0-9]+)/?$ profile.php?name=$1 [L,QSA]
RewriteCond %{THE_REQUEST} \s/+error(?:\.php)?\?id=([a-z,A-Z,0-9]+) [NC]
RewriteRule ^ error/%1? [R,L]
RewriteRule ^error/([a-z,A-Z,0-9]+)/?$ error.php?id=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME}\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php
ErrorDocument 400 /error/400
ErrorDocument 401 /error/401
ErrorDocument 403 /error/403
ErrorDocument 404 /error/404
ErrorDocument 500 /error/500
ErrorDocument 503 /error/503
Redirect /index.php /home
Redirect /index.html /home
RewriteRule /sitemap.xml /sitemap.xml.php
Bellow this rule:
RewriteRule ^resources/([a-z,A-Z,0-9]+)/?$ resources.php?id=$1 [L,QSA]
Add this rule:
RewriteRule ^resources/([a-z,A-Z,0-9]+)/([a-z,A-Z,0-9]+)/?$ resources.php?id=$1&name=$2 [L,QSA]

htaccess subdomain

Howto make this with htacess:
subdomain.domain.com -> domain.com/subdomain (no redirect on client side)
domain.com/subdomain -> subdomain.domain.com (redirect)
I make firs redirect:
RewriteRule ^subdomain/ - [L]
RewriteCond %{HTTP_HOST} ^subdomain.domain.com [NC]
RewriteRule (.*) subdomain/$1 [L]
How is it inserted in the form htacess:
AddDefaultCharset utf-8
DirectoryIndex index.php index.html index.htm
Options All -Indexes
ErrorDocument 404 /404.html
ErrorDocument 403 /404.html
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !\.(png|jpg|jpeg|gif)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ index.php/$1 [QSA]
This is a wildcard based solution, so it sould work for any number of subdomains.
This will redirect domain.com/foo/bar to foo.domain.com/bar:
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ([^/]+)(/.*|$) $1.domain.com/$2 [R=302]
This will handle (internal rewrite) the virtual hosts:
RewriteCond %{HTTP_HOST} ^(.*).domain.com$ [NC]
RewriteRule (.*) %1/$1 [L]
You might consider using 301 (permanent redirect) instead of the 302.
I strongly suggest you have a VirtualHost for your main site domain.com or www.domain.com and a separate one for handling the virtual subdomains.
For only a certain subdomain:
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule (subdomain)(/.*|$) $1.domain.com/$2 [R=302,L]
RewriteCond %{HTTP_HOST} ^(subdomain).domain.com$ [NC]
RewriteRule (.*) %1/$1 [L]

Resources