Can i use multiple RewriteRules for the same url .htaccess - html

My .htaccess
Options -Indexes
DirectorySlash Off
RewriteEngine On
RewriteRule ^about$ about/about_me.html [L]
Is it possible to do this
Options -Indexes
DirectorySlash Off
RewriteEngine On
RewriteRule ^about$ about/about_me.html [L]
#Using multiple RewriteRules with diffrent names for same url
RewriteRule ^About$ about/about_me.html [L]
RewriteRule ^ABOUT$ about/about_me.html [L]

RewriteRule ^about$ about/about_me.html [L]
#Using multiple RewriteRules with diffrent names for same url
RewriteRule ^About$ about/about_me.html [L]
RewriteRule ^ABOUT$ about/about_me.html [L]
Yes, you can do this, but... you shouldn't.
/about and /ABOUT are strictly speaking different URLs. (URLs are case-sensitive.) Google sees these as different URLs, so if /about and /ABOUT both serve the same content then you are potentially creating a duplicate content issue (whether that is actually going to be a problem or not is another matter).
Aside: You don't need multiple directives to do this, creating another directive for each variation. You can use the NC (nocase) flag on the RewriteRule directive to make it a case-insensitive match. For example:
RewriteRule ^about$ about/about_me.html [NC,L]
This will match about, ABOUT and AbOuT etc. and rewrite the request to about/about_me.html in all cases.
Case-insensitive and avoid duplicate content - Redirect instead
To allow all cased variations of about to be accessible and avoid the duplicate content issue you should externally redirect (not "rewrite") the non-canonical variations (eg. ABOUT and AbOuT etc.) to the canonical about. And only rewrite about to about/about_me.html.
For example, before the above "rewrite" you could implement the following "redirect" to canonicalize ABOUT and AbOuT to about.
# Redirect "ABOUT" and "AbOuT" to "about"
RewriteCond %{REQUEST_URI} ^/about$ [NC]
RewriteRule [A-Z] /about [R=301,L]
However, this rule is specific to one URL - it is not very practical if you have more than a handful of URLs. You might instead choose to simply lowercase all requested URLs. For example:
How to rewrite URLs from UPPERCASE to lowercase in .htaccess

Related

Prevent direct URL access with .htaccess

I know this question has been asked before several times, but I couldn't find the right answer.
I would like to allow search engines and some referrers to access a certain URL, without allowing direct URL access.
You can't reach this domain by clicking the link to
2betist.umran.org
When you search this domain on Google, you can reach the website by clicking the search result link, but accessing it directly via URL or via referrer doesn't work. I would like to create a white list on .htaccess for some of the referrers, along with the Google and Bing search engines.
I hope I describe the problem clearly enough. Thanks!
I have googled and found https://support.acquia.com/hc/en-us/articles/360005257234-Introduction-to-htaccess-rewrite-rules which states:
The .htaccess file controls a number of ways that a website can be accessed, blocked, and redirected. It does this using a series of one or more rewrite rules. These rewrites are made possible by Apache's mod_rewrite module.
mod_rewrite provides a way to modify incoming URL requests, dynamically, based on regular expression rules. This allows you to map arbitrary URLs onto your internal URL structure in any way you like.
The basic formulation of any .htaccess rewrite rule includes setting a combination of rewrite condition (RewriteCond) tests along with a corresponding rule (RewriteRule) if the prior conditions pass. In most cases, these rules should be placed at any point after the RewriteEngine on line in the .htaccessfile located in the website's docroot.
The keys are htaccess rewrite conditions and rules.
Another website provided an example for a .htaccess file to disallow certain referrers.
RewriteCond %{QUERY_STRING} / [OR]
RewriteCond %{HTTP_REFERER} \.semalt\.com [OR,NC]
RewriteCond %{HTTP_REFERER} best-seo-solution\.com [OR,NC]
RewriteCond %{HTTP_REFERER} best-seo-offer\.com [OR,NC]
...
RewriteCond %{HTTP_USER_AGENT} OrgProbe [OR,NC]
RewriteCond %{HTTP_USER_AGENT} Majestic [NC]
RewriteRule ^.*$ - [F,L]
Here you can find the complete Apache documentation.

Use htaccess to fix misspelled urls

So I have a pretty simple problem (at least I think do) with my website. I need to be able to redirect any misspelled URLs to the correct ones. It's easier if I explain it to you guys than to describe it.
For example, let's take this url.
http://www.tomshardware.com/reviews/radeon-r9-290x-hawaii-review,3650.html
Now, that url will take you to the correct page of that article regardless of how the url is spelled. Say you accidentally place a letter, number or a word into that URL to something like this:
http://www.tomshardware.com/reviews/radeon-r9-290x-TEST-TEST-hawaii-review,3650.html
That url will still take you to the correct article and fix itself to the correct URL. You could add anything to that URL and it will still take you to the right article regardless what you accidentally type into it.
So my question is how do I do this in htaccess? This is my current htaccess file
# Secure htaccess file
<files .htaccess>
order allow,deny
deny from all
</files>
AddHandler application/x-httpd-php5 .html .htm
AddType application/x-httpd-php .html .htm .php
AddHandler cgi-script .pl .cgi
Options ALL -Indexes -Multiviews +ExecCGI +FollowSymLinks
# Do not remove this line, otherwise mod_rewrite rules will stop working
RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
#Redirect Non-WWW to WWW
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} /index\.html?$ [NC]
RewriteRule ^(.*)index\.html?$ "/$1" [NC,R=301,NE,L]
You probably can't do that in that way.
As you can observe, the text on the url is totally irrelevant and is only there to create readable and index-friendly (SEO) urls. Those words are called "slugs", see http://en.wikipedia.org/wiki/Clean_URL#Slug
If you modify the last part, the 3650 it will break the url because this is the only identifier which typically corresponds to a unique ID in the database.
Assumption on how and why the mentioned site do this:
The site uses either a standalone routing component (e.g. Routing from Symfony PHP framework: http://symfony.com/components/Routing), an entire web framework or everything is written by hand. Depending on the language it might be ZEND, Symfony, etc for PHP, MVC for Asp.net or any other.
In all cases there is some sort of filtering of urls before the original content is served.
The routing parses the url, retrieves the unique ID, fetches the data set and creates again an absolute URL out of it.
It then compares the freshly generated route with the one you have entered.
If they don't match the framework issues a http status of 30x and redirects you to the new url.
The purpose of that is to maintain link sanity when the slug tags have changed or for whatever reason the SEO friendly url layout have changed.
The redirect is there so the old fashioned urls are updated next time a search engine visits the page and updates it's index.
Imagine you have a typo somewhere in the slugs or you forgot to mention Radeon and you want to avoid having it forever broken or wrong in the DB.
So you need to fix it but at the same time you want to avoid breaking the old urls for search indexes which have not yet revisited your site with the new slugs or users that have bookmarked it.
After the redirect it again compares the urls and after they match the content is served.
A DB lookup is very likely here and you cannot do this properly with htaccess alone as you have no knowledge about correctness of the url here.
You would internal-redirect all article pages to a php program and it will match the parameters with best possible page to show
-- .htaccess --
RewriteEngine on
RewriteRule ^article/(.*).html$ /article.php?url=$1 [L]
-- php --
read article selection criteria
$article_url=$_GET['url'];
Search through database or files and show the article

htaccess rewrite allcaps.html to lowercase.html

I've been trying to figure out how to get this to work for awhile now and it's not making any sense to me. What I've put together so far forces it to reference the lowercase.html file from /BLAH to /blah.html but I can't seem to put together /BLAH.html to /blah.html.
This is only to move SomeCapS.html (or .HTML I guess) to somecaps.html instead.
Any ideas on how to fix this code:
RewriteEngine On
RewriteBase /
# http://www.askapache.com/htaccess/rewrite-uppercase-lowercase.html
# If there are caps, set HASCAPS to true and skip next rule
RewriteRule [A-Z] - [E=HASCAPS:TRUE,S=1]
# Skip this entire section if no uppercase letters in requested URL
RewriteRule ![A-Z] - [S=28]
# Replace single occurance of CAP with cap, then process next Rule.
RewriteRule ^([^A]*)A(.*)$ $1a$2
RewriteRule ^([^B]*)B(.*)$ $1b$2
RewriteRule ^([^C]*)C(.*)$ $1c$2
RewriteRule ^([^D]*)D(.*)$ $1d$2
RewriteRule ^([^E]*)E(.*)$ $1e$2
RewriteRule ^([^F]*)F(.*)$ $1f$2
RewriteRule ^([^G]*)G(.*)$ $1g$2
RewriteRule ^([^H]*)H(.*)$ $1h$2
RewriteRule ^([^I]*)I(.*)$ $1i$2
RewriteRule ^([^J]*)J(.*)$ $1j$2
RewriteRule ^([^K]*)K(.*)$ $1k$2
RewriteRule ^([^L]*)L(.*)$ $1l$2
RewriteRule ^([^M]*)M(.*)$ $1m$2
RewriteRule ^([^N]*)N(.*)$ $1n$2
RewriteRule ^([^O]*)O(.*)$ $1o$2
RewriteRule ^([^P]*)P(.*)$ $1p$2
RewriteRule ^([^Q]*)Q(.*)$ $1q$2
RewriteRule ^([^R]*)R(.*)$ $1r$2
RewriteRule ^([^S]*)S(.*)$ $1s$2
RewriteRule ^([^T]*)T(.*)$ $1t$2
RewriteRule ^([^U]*)U(.*)$ $1u$2
RewriteRule ^([^V]*)V(.*)$ $1v$2
RewriteRule ^([^W]*)W(.*)$ $1w$2
RewriteRule ^([^X]*)X(.*)$ $1x$2
RewriteRule ^([^Y]*)Y(.*)$ $1y$2
RewriteRule ^([^Z]*)Z(.*)$ $1z$2
# If there are any uppercase letters, restart at very first RewriteRule in file.
RewriteRule [A-Z] - [N]
RewriteCond %{ENV:HASCAPS} TRUE
RewriteRule ^/?(.*)$ /$1.html
Thanks a lot
You need to use the built in rewrite map directive, because this has a lot of problems. First, the number of internal redirects is capped at something like 10. This means you can only redirect internally 10 times, and a URI with 11 upper case letters will cause too many internal redirects, resulting in a 500 server error.
The Apache documentation has a page for RewriteMaps.
But the problem is rewrite maps must be declared in the vhost/server config. And not in the htaccess file. If you don't have access, you may need to write a script to do this for you. Something like:
RewriteEngine On
RewriteRule [A-Z] /tolower.php [L]
And in the tolower.php you'd need to look at $_SERVER['REQUEST_URI'], then change all the letters to lower case, then either redirect the browser to the new URL without any upper case letters or, internally load that page and return it on the browser's behalf.

htaccess rewrite rule url

I want to write htaccess rules where I can rewrite this
www.example.com/Project_Name/1/23 project.html?n=$1&p=$2&i=$3​​​​​​​​​​​​​​
I have this already, but the problem is that I have a file in /projects/projects.xml which gets rewritten as well when I call it with Ajax.
RewriteRule ^([\._\-a-zA-Z0-9]*)\/([0-9]*)\/([0-9]*)(.?)$ /project.html?n=$1&p=$2&i=$3 [NC]
Try adding RewriteCond %{REQUEST_FILENAME} !-f above the RewriteRule. Assuming projects.xml is an actual file, that should prevent rewriting.

How to avoid double google indexing using .htaccess?

I have a website, with a nice RewriteRule in its root, that redirects all the queries of this kind:
http://domain.com/foo/parameter
into
http://domain.com/index.php?args=parameter
Users can only see the clean URL and everyone is happy.
Now here is the problem: domain.com DNS have an A record for domain.com, pointing to a private server IP, and an A record for mail.domain.com, pointing to the exact same IP.
For some unknown reason, in the last couple of months, Google double indexed all the pages of my site (http://domain.com/foo/par1, http://domain.com/foo/par2 etc.) with another set with the mail subdomain (http://mail.domain.com/foo/par1, http://mail.domain.com/foo/par2 etc).
I thought I could get rid of all of them redirecting any request to mail.domain.com/$whatever to domain.com and eventually Google would understand that all those pages with the 'mail' subdomain redirects to the homepage and are therefore not necessary.
I tried this in .htaccess:
RewriteCond %{HTTP_HOST} ^mail.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com [R=301,L]
But this redirects to a visible URL that looks like this: http://domain.com/index.php?args=parameter, while I just want a redirect to the homepage.
What's the correct form, and are there more elegant ways to achieve this, maybe adding something into robots.txt? (Please note that I can't just disallow a subfolder here)
If you just want to redirect to home page by discarding the original REQUEST_URI and QUERY_STRING then use these rules:
RewriteCond %{HTTP_HOST} ^mail.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/? [R=301,L]
By putting ? in the end it will strip out original query string, thus a URL of this type: http://mail.domain.com/index.php?args=parameter will become http://domain.com/
Your rule is correct, but you need to put it before all the other rules (right after RewriteEngine On) or it will pick up the latest state of the internal rewritten URL.
Update: Hmm, you said that your old rule redirects correctly but is using the internal, ugly, URL. That actually shouldn't be the case unless you add $1 to pick out the matched string.
RewriteCond %{HTTP_HOST} ^mail.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]