Use .htaccess to allow subfolders both with and without trailing slash - .htaccess

I am about to move my html files from the root folder to a couple of subfolders.
The default setting my host is using is to show the trailing slash for the subfolders and no slash for the html files. If the browser requests a subfolder without trailing slash, a 301 redirect to the same folder with trailing slash is returned. The files are shown without html extension thanks to a .htaccess snippet.
I think I like the approach to distinguish the subfolders from the files. In my case the contents of the index files will be different than the ordinary files.
However, I am not as convinced as many others that duplicate content is a disaster. I cannot be sure how my websites will develop in the future and what I will think about trailing slashes and such, so an easy forward compatible strategy is therefore desirable. For that reason I am considering to abandon the 301 redirect and instead allow the browser to send urls to subfolders both with and without the trailing slash and with response 200 OK in both cases. Yes, that leads to duplicate content, but I will will keep the trailing slash on all subfolders on all internal and canonical links, so in practice, I do not think it is a disaster. And Google and Bing and others should be able to figure things out.
I have my sites on a shared server configured via directadmin and (probably) no access to other configuration files and do not know how the default setting has been accomplished. I guess I need to do something in .htaccess to change the default setting but I am not sure what.
Is there anybody who knows how to override the default setting so that I get rid of the 301 redirect in the mentioned case and instead send 200?
I found this interesting document https://httpd.apache.org/docs/2.4/mod/mod_dir.html#directoryindex , but I can not interpret it, and I do not dare to do any trial and error. Hope that somebody can give me a hint.

Related

redirect numerous dynamic urls to home page via .htaccess

I am trying to clean up a previously hacked WordPress site, and domain name reputation, the site has new hosting and is now on a different CMS system, but there are hundreds of spam links in Google I need to get rid of, they look like example.com/votes.php?10054nzwzm75042pw205039
Domain name, then votes.php?**** etc.. Numbers letters all sorts.
So how do I redirect ANYTHING that starts with the domain name then /votes.php?***
Any help greatly appreciated
Unless you have multiple domains, you don't need to explicitly check the domain name.
To send a "410 Gone" for anything that contains /votes.php in the URL-path (and any query string), you can do something like the following at the top of your root .htaccess file using mod_rewrite:
RewriteEngine On
# Serve a 410 Gone for any requests to "/votes.php"
RewriteRule ^votes\.php$ - [G]
A 410 is preferable to a "redirect" if you want to get these URLs removed from the search engines as quickly as possible.
To expedite the process of URL removal from Google then use Google's Removal Tool as well.
If you redirect these pages to the homepage then it will likely be seen as a soft-404 by Google and these URLs are likely to remain in the search results for a lot longer.

How to Redirect Slash at the end of my domain?

In my Google webmaster tools Google sees http://www.copperreflections.com/ separate from http://www.copperreflections.com How can I fix this this?
You can't by redirecting/rewriting because as far as the server knows,http://www.copperreflections.com/ and http://www.copperreflections.com are identical, the requested URI is / for both of them. You'll have to make sure that your pages use one with a trailing slash when linking to "copperreflections.com".
You may also be able to use javascript to redirect the browser when the address bar has the URL without the trailing slash to one with the trailing slash.

URL Rewriting with htaccess

if iam giving my url as http://localhost/york/aboutus.php it is working fine, but the problem i am having is that if i provide http://localhost/york/aboutus.php/ or any character after the trailing slash of the above url and then if load that page then all the css and javascript files are not working. Please provide me the solution to this problem.. Thanks in advance..
The problem is most likely the way you are including the javascript / css files in your page. With more details you will probably get a more detailed answer, but I would guess you are using .htaccess to rewrite the page in question and then in your html you are using a relative path for the src/link attributes of js/css. Every forward slash in your URL, the clients browser will assume is a directory and it will calculate relative paths accordingly.
The solution is to provide all your src/js paths relative to Root not your cwd. That is to say they should begin with a forward slash and then have the full path from your root directory. This way the clients browser will request http://www.domain.com/path/to/style.css, regardless of how many slashes are in the pages URL.

Why does IIS 7.5 adds a trailing slash on folders? Can we disable courtesy redirect for a URL Rewrite rule that removes trailing slash?

IIS does URL cleanup on directories by adding a trailing slash. See this old docs from IIS 6:
IIS generates courtesy redirect when folder without trailing slash is requested
Why? Is the intent still relevant?
Any security implications?
How can I disable it to make this work with a URL Rewrite rule "RemoveTrailingSlashRule"
When you add a rule under IIS 7.5 with URL Rewrite 2, the rule will not be applied to directories (using IsDirectory) and folders (using IsFolder).
See this warning on Add a rule to append or remove the trailing slash symbol:
This will create the RemoveTrailingSlashRule1:
I have an answer for the specific case of a child IIS Application here: https://stackoverflow.com/a/25817317/292060. The child app seems to be the usual culprit, but isn't explicitly described in this question.
To try to answer the questions, here are my opinions from dealing with IIS and Microsoft for years. I don't have hard sources to cite; some of this is just gut feelings.
Why? Is the intent still relevant?
I think it stemmed from the original "default document" feature, namely index.html. Websites wanted their home page to just be the domain, then this extended to subfolders. With url rewriting, the intent isn't relevant anymore - you can rewrite to your heart's content, and would rather IIS get out of the way. It's common to want friendly urls, and no trailing slash (except for the domain/website root - that is required to have a trailing slash, even if some browsers like Chrome get cute and hide it).
Any security implications?
I think the only security implication was the original directory browsing. If you forgot to do a default document, and directory browsing was left turned on, then people could browse your website files. As far as I know, directory browsing has been long disabled as the default setting.
With any requests, whether trailing slash or not, url rewriting or not, your server and code need to withstand bad requests. This is true for all situations, not just specific to the slashes. http://xkcd.com/327/
How can I disable it to make this work with a URL Rewrite rule "RemoveTrailingSlashRule"
I have an answer if the issue is the child application, here: https://stackoverflow.com/a/25817317/292060
The summary is, in IIS:
Disable the Default Document feature for the child application.
Using Url Rewrite, create a rule to rewrite (not redirect) an empty request to default.aspx
If this question is for a more general issue, including regular subfolders even if not a child app, consider removing the "Is Not a Directory" from the rule, and let this redirect even when it sees a directory. That may work, or may create an infinite redirect loop, I'm not sure.

Changing website name using mod_rewrite and htaccess

I want to rename a folder on my site from http://mywebsite.com/myfolder/ to http://mywebsite.com/mynewfolder/. The urls for the old folder name are all index by Google and may other sites have linked to mine. What is the correct way to ensure that visitors coming to the site on the old folder name will now see the new folder name? Should I chane the name of the folder on my server and then use mod_rewrite to force the new url (folder name)
this seems to work, but is it correct: Redirect 301 /myfolder /mynewfolder
also for SEO would it be better to use: /my-folder-name/
RewriteEngine on
RewriteRule ^oldfolder/ /newfolder/ [R=301,NC]
It is widely acknowledged that hyphenating (-) your URLs makes a small impact on SEO as it separates any keywords in your URL rather than having them read as one long string. However saying that I'm pretty sure Google is clever enough to have a go at working this out for themselves. I don't suppose it would hurt and it makes it easier for your user to read at the very least.

Resources