I need to make accessing directories on my server case insensitive.
How do I do that using htaccess?
You have to install and enable the mod_speling module in apache and set the CheckCaseOnly Directive to On in your .htaccess
CheckCaseOnly On
If you want requested URLs to be valid whether uppercase or lowercase letters are used, use mod_speling to make URLs case-insensitive. Write the following code in .htaccess file:
CheckSpelling On
This is what I used because my hosting is shared and does not include the mod_spelling module but does support .htaccess, but this only works for one folder:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^student-government/$ http://www.tombarrasso.com/Student-Government/ [R=302,NC,L]
The folder to redirect to can be any case, so you could use lower-case folders and redirect all variations of spelling there.
I suppose it could be adapted with a little bit of REGEX to work for all folders rather than just one. This worked for me on Apache 2.2.14 (Unix).
Solution-1: To make case-insensitive directory and files names with respect to the requested URL we can add the following two lines to the app's .htaccess file:
<IfModule mod_speling.c>
#Once enabled, mod_speling redirects misspelled requests to any nearest matching resources. Uses a bit of memory, but can be useful if you've been changing URIs or have lots of similarly named URIs:
CheckSpelling On
CheckCaseOnly on
</IfModule>
Solution-2:
Again if we want just a few predefined directories to go to specific directories, then apply the below lines instead:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^my-dir/old-dir-1/$ /my-dir/new-dir-1/ [R=301,L,NE]
RewriteRule ^my-dir/old-dir-2/$ /my-dir/new-dir-2/ [R=301,L,NE]
RewriteRule ^my-dir/old-dir-3/$ /my-dir/new-dir-3/ [R=301,L,NE]
# For any case insensitive directories we can try adding NO-CASE (NC)
RewriteRule ^My-Old-Dir/$ /my-new-dir/ [R=301,L,NC]
</IfModule>
Related
I want to rewrite multiple sub-directories to the same path. while I can remove a part from URL Apache htaccess
for example, I want
domain.com/category/computer/internet-topics
domain.com/category/computer/windows-topics
domain.com/category/science/physics-topics
domain.com/category/science/biology-topics
to be
domain.com/category/internet-topics
domain.com/category/windows-topics
domain.com/category/physics-topics
domain.com/category/biology-topics
When I use this, it works fine for the first line only (computer)
RewriteEngine On
RewriteRule ^category/([a-zA-Z0-9_-]+)$ category/computer/$1
RewriteRule ^category/([a-zA-Z0-9_-]+)$ category/science/$1
I think you need to be specific else only first rule will be used
RewriteEngine On
RewriteRule ^category/((internet)|(windows)-topics)$ category/computer/$1
RewriteRule ^category/((physics)|(biology)-topics)$ category/science/$1
I am making a website builder an I would like to make urls prettier.
The url would be for example:
https://ccc-bb.example.com => https://example.com/project/show/ccc/bb
This is my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
# prevents files starting with dot to be viewed by browser
RewriteRule /\.|^\.(?!well-known/) - [F]
# front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\-(.*)$ https://example.com/project/show/$1/$2 [L]
When I use above (https://ccc-bb.example.com) it sends me to the subdomain empty folder. The folder has only the .htaccess file.
What am I missing? I've never edited an .htaccess file and Google didn't help me (or I don't know what should I looking for).
Your first rule for dotfiles is okay but would be better the other way around, since the second part can only match the start, but the first can only match in subdirectories.
RewriteRule ^\.(?!well-known/)|/\. - [F]
Your other rule's problem is that you are expecting it to match the subdomain. RewriteRules do not operate on the entire string you see in your browser's address bar, only the path part, and in .htaccess they see even less as the leading directory is stripped off, too. To access the info you want, use a RewriteCond:
RewriteCond %{HTTP_HOST} ^([^-]++)-([^-.]++)\.example\.com$
RewriteRule ^(?!project/show/).* project/show/%1/%2/$0 [L,DPI]
(You don't need to include \.example\.com$ if your main domain contains no hyphens.)
I'm not sure what's wrong. I use massively rewrite on to domain with various rules with no problem, now here on subdomain the rewritten result point wrong data.
The space is accessible from multiple domains, and I want to switch by hostname to specific subfolder for CSS contents:
RewriteEngine on
# - - - shared space / multiple css.*.tld subdomains - - -
# domain 1
RewriteCond %{HTTP_HOST} ^css\.firstdomain\.com$
RewriteRule ^css/(.*)$ fir/$1
# domain 2
RewriteCond %{HTTP_HOST} ^css\.seconddomain2\.com$
RewriteRule ^css/(.*)$ sec/$1
# *.min.css -> scss.php?file=*
RewriteRule ^(.+)\.min\.css$ scss.php?scss=$1 [QSA]
#
First there are rules to rewrite universal /css/ path to path specific for files for that host, then last line should change *.min.css to scss.php?scss=$1 - so it send the file /without the extension .min.css/ as parameter to php file which then searches for that file with .scss extension to check last modified and either return cached or recompile the source scss file to cached css file
Now I'd expect when I enter: http://css.firstdomain.com/css/first/first.min.css
should rewrite to: http://css.firstdomain.com/scss.php?file=fir/first/first.min.css
but it rewrites to: http://css.firstdomain.com/scss.php?file=fir/first/first.min.css/first/first
So it rewrites it like almost twice for some reason. What might be the reason for this?
There will be problem of some kind with the firs part as entering straight http://css.firstdomain.com/fir/first/first.min.css
rewrites to correct (scss.php?file=fir/first/first).
Oooh found it! I missed the [L] (last) flag after rules for each domain, now it works like it should
For completeness, here is the code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^css\.firstdomain\.com$
RewriteRule ^css/(.*)$ fir/$1 [QSA,L]
RewriteRule ^(.+)\.min\.css$ scss.php?scss=$1 [QSA,L]
I have a site with a folder, and a htaccess file within that folder. For the index.php file within that folder, I want to rewrite the querystring for a certain parameter, so that typing in this URL:
www.example.com/myfolder/myparameter
Behaves like this (ie makes $_GET['parameter'] = 'myparameter' in my code)
www.example.com/myfolder/index.php?parameter=myparameter
I have looked at many questions on StackOverflow, but have not managed to get this working. My code so far is
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*) [NC]
RewriteRule ^$ %0 [QSA]
But that just isn't working at all.
Please use this code
RewriteEngine on
RewriteRule (.*) index\.php?parameter=$1 [L,QSA]
RewriteEngine on
RewriteRule (^.*/)([^/]+)$ $1index\.php?parameter=$2 [L,QSA]
update
sorry use #somasundaram's answer. Per-directory .htaccess rewrite rules lose the directory prefix:
When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the RewriteRule pattern matching and automatically added after any relative (not starting with a slash or protocol name) substitution encounters the end of a rule set. See the RewriteBase directive for more information regarding what prefix will be added back to relative substitutions.
(from the apache docs)
I'm looking to rewrite the first directory of a url string and have the rest of the request still work.
Eg: I want it so when a user clicks the link for : /products/category/item.php
it actually grabs the file of : /shop/category/item.php But still shows as /products/category/item.php as the URL
This will be dynamic so it should be something like /products/$ /shop/$1 I'm guessing.
You do not need mod_rewrite. when to avoid mod_rewrite.
Mapping url directories to file directories is a basic functionnality of Apache handled by the mode mod_alias (which is quite certainly already present for you).
So basically you have the Alias and AliasMatch directives. In your case the first one is enough:
Alias /products/ /path/to/web/document/root/shop/
The mapping is done only server-side so the url seen by the end user is never modified.
Try this:
RewriteEngine On
RewriteRule ^products/(.*) shop/$1 [L]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^products/(.+)$ /shop/$1 [L,NC]