RewriteRule of htaccess file gives a 404 error - .htaccess

I've the problem that my .htaccess RewriteRule gives me a 404 error page no matter what I'm trying.
My files are located inside the /df-talents directory on the server. Inside this folder the .htaccess file is located, too.
I need a RewriteRule which redirects the URL to /df-talents/index.html when the actual URL has the word "classes" included.
This is the content of the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !(.html|.js|.css)
RewriteCond %{REQUEST_URI} !^(.*).(jpg|jpeg|png|gif|txt|ico|webp)
RewriteCond %{REQUEST_URI} !(.eot|.ttf|.woff)
RewriteRule ^(.*)classes(.*)$ index.html [NC,L]

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]

htaccess 404 page for specific subdirectory only (static html pages)

Stuck again with htaccess. How can I add a 404 page to a specific subdirectory?
In my case the 404 page should only affect the "staging" subdirectory (and the directories inside of staging) but not the root or any other subdirectories placed in root.
http://example.com/staging
The code which is in my .htaccess file for the directory "staging":
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
Options -Indexes -MultiViews
RewriteEngine on
RewriteRule ^arbeiten/$ /staging/arbeiten/alle [R=301,L]
RewriteRule ^journal/$ /staging/journal/journal-alle [R=301,L]
ErrorDocument 404 /staging/404.html
Using the code above (and many others) results in a Internal Server Error.
Update #1 to my initial question.
In general, the code/answer provided to add a 404 not found page to a specific sub directory works:
ErrorDocument 404 /staging/404.html
The reason why it still does not work is because there must be some error in the overall .htaccess setup related to the code remove .html extension and/or redirect to a page in a folder.
Options -Indexes -MultiViews
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
RewriteRule ^arbeiten/$ /staging/arbeiten/alle [R=301,L]
RewriteRule ^journal/$ /staging/journal/journal-alle [R=301,L]
ErrorDocument 404 /at/404.html
I will continue to further investigate the setup in my .htaccess.
Update #2
Using the .htaccess script below allows to remove .html extensions, redirect pages to a specific page within a directory and basically a working 404 not found page within a specific sub directory on a Hostgator cPanel grid hosting (apache).
Options -Indexes -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]
RewriteRule ^arbeiten/$ /staging/arbeiten/alle [R=301,L]
RewriteRule ^journal/$ /staging/journal/journal-alle [R=301,L]
ErrorDocument 404 http://www.example.com/staging/404
Remaining 404 problem:
http://example.com/staging/blabla (404 works)
http://example.com/staging/folder/blala (404 works)
http://example.com/staging/folder/project-detail-pagexxx (404 works)
http://example.com/staging/folder/project-detail-pagexxx/ (404 does not work)

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 /

Remove .html extension from URL on subdomain files

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.

Hidden redirect of / to subfolder using .htaccess in TYPO3

We're setting up a TYPO3 installation, and if the user calls example.com/ we'd like the server to redirect to /typo/index.php?id=106.
This should happen without a change in the address bar. Every other file access on the server (for example example.com/test.png) should be redirected to example.com/typo/test.png).
This is the .htaccess file in the root directory. As I understand, it will redirect everything which doesn't have /typo in the URL to the subfolder and attach the parameters:
RewriteCond %{REQUEST_URI} !^/typo/
RewriteRule ^(.*)$ typo/$1 [L]
Now, this already seems to work, when I call example.com/index.php?id=106 I'm not getting a 404. Unfortunately TYPO3 seems to have some trouble (or the .htaccess configuration isn't correct), because we get a message saying "No input file specified".
What's also missing is the initial redirect when no path is specified. It should then go to /typo/index.php?id=106.
You may try this in one .htaccess file in root directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
# URL with no path
RewriteCond %{REQUEST_URI} ^/?$ [NC]
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule .* /typo/index.php?id=106 [NC,L]
# URL with path
RewriteCond %{REQUEST_URI} !^/typo [NC]
RewriteRule ^(.+) /typo/$1 [NC,L]
Maps silently:
http://domain.com/ to
http://domain.com/typo/index.php?id=106
and
http://domain.com/anything
http://domain.com/typo/anything
For permanent redirection, replace [NC,L] with [R=301,NC,L]

Resources