301 redirect for html files in one directory only to Custom Post Type in Wordpress - html

I am struggling to get my head around an htaccess rule to redirect requests for an html file to go to a custom post. I have looked on here and in other places and nearly got there.
I want to redirect ONLY mydomain.com/profiles/.html to mydomain.com/name_profile/
So mydomain.com/profiles/smith.html to mydomain.com/name_profiles/smith. There are some 900 html files to be redirected and they are all contained in this directory. Other html files in the domain I need to leave correctly associated.
I currently have
RedirectMatch 301 ^/([^/]+)/([^/.]+)\.html$ /$1/$2/
RedirectMatch 301 ^/([^/]+)/([^/]+)/([^/.]+)\.html$ /$1/$2/$3/
But this redirects all html pages not just the ones in the profiles directory.
I am new at htaccess and have found several tutorials but none at a level I can understand, so any help is most welcome.

Place this rule just below RewriteEngine On line:
RewriteRule ^profiles/([^.]+)\.html$ /name_profiles/$1 [L,NC,R=301]

Use this rule:
RewriteEngine On
RewriteRule ^profiles/([^.]+)\.html/? name_profiles/$1 [DPI,L,R]
Or better, remove your RewriteMatch rules and replace with this:
RewriteEngine On
RewriteRule ^([^/]+)/([^/.]+)\.html/? $1/$2 [DPI,L,R]
RewriteRule ^([^/]+)/([^/]+)/([^/.]+)\.html/? $1/$2/$3 [DPI,L,R]
This assumes that mod_rewrite is both installed and activated for htaccess files.
If you are not sure, to check if mod_rewrite is installed, look at the list of installed modules in the output of phpinfo();
By default, mod_rewrite is not enabled for htaccess files. If you are managing your own server, open httpd.conf
and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All

Related

Pointing webserver to load .html page and not subdirectory index

So I have been searching for a few hours now and have not found the answer to my problem.
So I have a website that I was wanting to take the page extension off e.g. .html / .php etc.. and I had found that I could include the below code in the .htaccess file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
I have now added that code and it works great for removing .html from my pages. However now I have come into a new problem. I have a page and directory both named 'guides' on the root of my public_html folder. Now when I enter the url 'www.mydomain.com/guides' the index of the guides directory is showing rather than the guides.html page that I am wanting to display.
I am wondering what would be the best way to get around this and make my server look for the webpage first rather than display the guides directory index.
I have been looking at RewriteRule's but it seems to be all wordpress content and does not work with my non wordpress website.
Any help would be appreciated, thanks.
The best solution is to change the name of the directory or the file to something different. RewriteRule can not rewrite a directory request to file as the directory request is first handled by mod-dir (directory module) .
If you do not want to change the name, then you can turn off DirectorySlash on your server using the following line in htaccess :
DirectorySlash off
https://httpd.apache.org/docs/2.4/mod/mod_dir.html#directoryslash

.htaccess file does not overwrite html and instead shows markup on page

I'm building a personal website, and I want my url links to look like domainname.com/example as opposed to domainname.com/example.html
I'm using fastcomet for my webhosting service. Under my public_html folder I have a .htaccess file. Here is the code for my .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
</IfModule>
My .htaccess file and all my html, css, js files are under home/username/public_html/ and I've removed the .html extensions from all my html files. After I applied these changes, I tried to get http://domainname.com/example and I saw the html markup as opposed to the rendered html such as:
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<all my code .... />
</html>
Any suggestions on what I should do? I am placing the .htaccess file in the wrong directory? I also tried with and without the Ifmodule tags on the .htaccess file.
Thanks!
Yes, indeed, you should keep the file extension .html so you can have your files properly handled and your .htaccess to work as intended. It seems that in the meantime, you have actually posted the ticket to our Tech Support Team. Though there has been a slight misunderstanding regarding your request, a senior member of our team successfully excluded the processing of HTML and HTM files from the NginX service and they are now passed directly to Apache for your domain. As a result, all of your url links now look like domainname.com/example as opposed to domainname.com/example.html. Should you have any further requests, do not hesitate to contact us back!
I've removed the .html extensions from all my html files
This is why you see page code.
Rename the files back and use the following rewrite rule:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
</IfModule>
FastComet Team here! Part of our shared hosting environment is utilizing NginX as a reverse proxy to the Apache web service. This means that static content, such as HTML files, is processed directly by NginX and not by Apache. This is why any rules that you apply to the .htaccess file of your website are not making any actual changes. The requests to HTML files will be processed directly by NginX and they will not be passed to Apache so the .htaccess rules will be ignored.
There is an easy way of resolving that. We can exclude the processing of HTML and HTM files for your domain or even your entire hosting account. This way, those requests will be processed by the Apache web server, instead of NginX. In this case, the .htaccess rules that you apply will be taken into consideration by the system and they will work without any issues. To achieve that, simply submit a ticket to our Technical Support team and we would be happy to assist.

Redirecting nonexistent index.html to home page in Wordpress is causing too many redirects in browser

I recently changed my website from static html to Wordpress, and in doing so I've been redirecting all of my former and nonexistent pages with my .htaccess file whenever Google shows me a crawl error. I've been successful in redirecting all crawl errors until today. My old index.html is throwing a crawl error and when I use:
Redirect 301 /index.html http://www.example.com
... I get this from my browser:
Too many redirects occurred trying to open www.example.com. This might occur if you open a page that is redirected to open another page which then is redirected to open the original page.*
I have since removed the above redirect from my .htaccess file and will just live with the crawl error if I can't get this resolved. But I'm thinking somebody knows how to solve this and if so, I'd really appreciate it if you'd let me know how.
You could experience this redirect loop if your DirectoryIndex is set to index.html (as the first option), which is likely to be the default setting on your server.
Basically, when you access a directory, eg. http://example.com/ (the root directory) then the DirectoryIndex directive tells the server which file to serve (via an internal rewrite). eg. http://example.com/index.html. This then seeds the redirect loop.
Since you are using WordPress, you could change this in .htaccess to favour index.php instead. For example, at the top of your .htaccess file:
DirectoryIndex index.php index.html
However, you could also solve this by using mod_rewrite (which is probably preferable). In fact, since WordPress already uses mod_rewrite (eg. RewriteRule directive) then you should also be using mod_rewrite for your redirects, not mod_alias (eg. Redirect directive). You should not mix redirects from both modules in .htaccess since you can easily get conflicts. Different modules execute at different times, regardless of their order in .htaccess.
By using mod_rewrite you can avoid a redirect loop by checking against THE_REQUEST. For example:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html [NC]
RewriteRule ^index\.html$ / [R=301,L]

Hiding a SINGLE File (Apahe)

I have a HTML file that is full of IP addresses from a PHP script that logs all IP's of vistiots. The only thing is that I don't want people to be able to go to
https:// mydomain.us/ips.html to see all of the IP's logged. What are my options to hide this ONE file.
I read all of these things with a code you put in your .htaccess, but none of them worked!
All I need is a way to hide this HTML file!
Sorry if this a bad question also, I'm really new to all of this :P
PHP Script:
(Used to write to the HTML File)
http://pastebin.com/jKDHeArb
Final Output (ips.html)
http://imgur.com/VWPmuSt
I suggest you put this page in a folder, and protect it with a password. There are many examples here or on the Internet.
But like you do not really want to block the page, but just to hide, You can do this by adding a code at the end of link. With this root .htaccess:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{QUERY_STRING} !^c=pass$ [NC]
RewriteRule ^ips\.html$ - [NC,F,L]
After it's possible to visit the page only with the following https link:
https:// mydomain.us/ips.html?c=pass
You can do the same in the php code.
you can add this to either an .htaccess or your Vhost
<Files ~ "ips.html">
Order allow,deny
Deny from all
</Files>

.htaccess file: is it possible to specify sub-directory location for rules?

My old website was built by handcoding HTML and has the directory structure:
public_html
.htaccess
index.html
...
content_folder
docs_folder
.htaccess
doc1.html
doc2.html
doc3.html
...
My new website is built using a CMS that stores its webpages in a database and so there is no traditional website directory structure like that shown above.
The public_html/docs_folder/.htaccess file above contains the following rules:
# Only permit access from links coming from my website (e.g.http://www.mydomain.com).
SetEnvIf Referer http://www.mydomain.com internal
<Files *>
order Deny,Allow
Deny from all
Allow from env=internal
</Files>
Since there's no directory structure in the new website, I wonder if there's anyway I can include the public_html/docs_folder/.htaccess rules above, which only apply to webpages in the docs_folder directory (and webpages in any sub-directories), inside the public_html/.htaccess file instead.
Is there a <location> attribute or other means to include a rule in an .htaccess file but have it apply to a sub-directory rather than the current directory the .htaccess file resides in?
Could someone give an example using the rule above?
Also, does <Files *> as used above cover all files (.html, .pdf, .css, etc.)? I'm hoping so.
Try adding this in the htaccess file in the root directory:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https?://yourdomain\.com [NC]
RewriteRule ^docs_folder/ - [L,F]
You'll need to remove the htaccess file in your /docs_folder/