Remove .html extension from URL on subdomain files - .htaccess

I have a problem with my .htaccess file. I would like to remove .html file extension on files that are in subdomain, but I can't do it.
Let's say that my website is david.xyz and subdomain calls sub (sub.david.xyz). This is my actual .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
Note: This .htaccess file works with non-subdomain files perfect.
Can you help me?

Ohh... I solved my problem:
If you want to create site sub.david.xyz/some-web/, you need to create in the root folder of your subdomain folder named some-web and then create index.html file in it. The "beautiful URL" will be created automatically.

Related

mod_rewrite a sub directoy to child subdirectory

I have a following sub domain and folder structure
https://subdomain.maindomain.com/companyName1/area/dashboard
I want to mod_rewrite to this
https://subdomain.maindomain.com/companyName1/area/
So that everything in "dashboard" loads, in "area"
I would like the .htaccess file to live in the area folder, as there are multiple companyNames
I've tried placing the following .htaccess in companyName1/area
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-s
RewriteCond %{DOCUMENT_ROOT}/dashboard/%{REQUEST_URI} -s
RewriteRule ^(.*)$ /dashboard/$1 [L]
However when visiting https://subdomain.maindomain.com/companyName1/area/ it just loads the index.html of that folder, and not of the /dashboard folder
With your shown samples, attempts please try following htaccess rules. Please make sure to keep these rules at the top of your htaccess file.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /companyName1/area/
##For external redirect to area link.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s.*/dashboard/?\s [NC]
RewriteRule ^(.*)/dashboard/?$ /$1/? [R=301,L]
##For internal rewrite to dashboard folder here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]

How to point my domain directly to a codeignter folder?

Let's say I have a domain www.example.com
Now I want that, at this domain, my index page or main controller of the CodeIgniter folder should open without the folder name and the rest controller pages also without the folder name.
From: www.example.com/folder
To: www.example.com
I am hosting my files on hostinger. I don't want to remove my folder and export all the structure to base public_html.
I want my public_html to contain my CodeIgniter folder.
Any help would be appreciated!
To setup codeigniter on a subdirectory, you could try the following htaccess rule :
Options +FollowSymLinks
RewriteEngine On
RewriteBase /YOUR_SUBFOLDER
RewriteRule ^$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php?/$1 [L]
Change the YOUR_SUBFOLDER above to whatever subdirectory name you have.

Redirect to homepage if file or directory doesn't exist using .htaccess

I am Hosting a domain on free hosting site 000webhost, I want to redirect all files or folder that doesn't exist to my homepage. The Code, I have used is:
RewriteEngine on
rewritecond %{http_host} ^domain\.com$ [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,l]
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.html
It works, but the url doesn't change to homepage. Like if I am trying to open www.domain.com/blaa it redirects me to home page but the URL is still www.domain.com/blaa. I want it to be changed to www.domain.com.
If I am trying to open a valid directory, which exist in my root folder, it also redirects to homepage. Like if I am trying to open www.domain.com/folder, it should result in files and directories in that folder, instead of that it redirects me to homepage and web address is still same like www.domain.com/folder.
Also I want to keep using the above 2 rules to open website with WWW and index.html with www.domain.com
Please help me.
Thanks in advance.
As per your questions's title, to redirect to homepage if the request is not for a file or dir, you can use :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ / [L]
You can also use ErrorDocument directive to rewrite non-existent requests to homepage
ErrorDocument 404 /

htaccess redirect root to subfolder - but keep other root subfolders beyond

My server has several system folders and files by default. so for better overview I want to put my website in a subfolder like this:
www.domain.de/web/index.html
ALL links and website-related stuff is found in this subfolder "web". BUT the folder "web" should not be seen in the url!
I got this with these lines in my htaccess and it works:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.de [NC]
RewriteCond %{REQUEST_URI} !^/web/.*$
RewriteRule ^(.*)$ /web/$1
But now the problem:
I also want other folder on my server to work. So www.domain.de/downloads/ should work, too. This folder is NOT in the subfolder "web". So if I write the direct url "www.domain.de/downloads" in my webbrowser, I don't want to get a redirect to "www.domain.de/web/downloads/", you know?
Is there any solution? :)
Thanks!
You Can use a RewriteCond to exclude your folder :
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.de [NC]
RewriteCond %{REQUEST_URI} !^/download [NC]
RewriteCond %{REQUEST_URI} !^/web/.*$
RewriteRule ^(.*)$ /web/$1

htaccess and rewriting urls

currently i have a /en/ folder that is empty except for a .htaccess with the following
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ ../index.php?language=en$1 [NC]
i use it to eliminate the need for
index.php?language=en
in all my URLs. I would like to modify the htaccess in a way that i no longer need the /en/ folder with nothing but the htaccess inside. ideally i would like an htaccess in my root folder that reads the url and if it is www.example.com/en/ to rewrite to www.example.com/index.php?language=en
This should work for you:
RewriteRule ^en/(.*)$ index.php?language=en$1 [NC]
Put the following code in .htaccess file in your root folder.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^en/(.*)$ index.php?language=en$1 [L]
Here $1 will append rest of the url as well. The condition will also help if you request your files using direct url.

Resources