Rewrite folder's name using htaccess - .htaccess

I am wondering wether it's possible to use .htaccess to rewrite a folder name. What I mean is this.
Lets say I have a url like:
www.example.org/anyname/page.php
Now I want to rewrite the url to (for example)
www.example.org/folder1/page.php
The folder1 is an existing folder on my webspace.
important: the "anyname" is not a folder rather just a name!
Ok here is a step by step plan:
User types www.site.com/anyname/login.php.
The url should rewrite and not redirect the url to www.site.com/folder1/login.php
This means that "anyname" is just a name and not a directory. All the code should just come from folder1. Acutally "anyname" should just be an alias for folder1. I can't just rename folder1 to "anyname". Therefore I would just rewrite folder1 to "anyname".

Try adding these rules to the htaccess file in your document root:
Options -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/.]+)\.php /folder1/$2.php [L]

Related

How to separate same directory - controller name in Codigniter

I've got a controller called course and a directory with the same name, and when I request
my_site/course
it redirects my to the directory course Instead of controller, how can I redirect to the controller
P.S It's very necessary to keep directory with the same name in my apllication
Does the directory need to be int he root folder, can it not be further down the hierarchy? By having a directory with the same name as your controller in the root directory, your web server is serving up the directory instead of normal CodeIgniter routing.
If it's an absolute must to have the folder in the root, then you would need to change your .htaccess file to ignore requests which match the directory name for that directory (and any others which match controller names).
There's an answer for this already, as well as a good example on the Drupal website which I've put in below in case it gets moved
=========[ start of .htaccess snippet]==========
<IfModule mod_rewrite.c>
RewriteEngine on
#
RewriteCond %{REQUEST_URI} !^/yourDirectoryName
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]
</IfModule>
====================[ end ]=====================
There nativ Ci routing.
In config/routes.php
$route['course'] = 'controller/course';
after default routes

What should I write in .htaccess file to rewrite this url?

I have a server parked on xyz.com. I have Wordpress installed on xyz.com/ and xyz.com/blog. I have created a new directory xyz.com/mamba.
In xyz.com/mamba. I want what that if user visits xyz.com/mamba/hello then the url should be rewritten to xyz.com/mamba/index.php?message=hello.
What should I write in .htaccess file in xyz.com/mamba/ directory?
Try:
RewriteEngine On
RewriteBase /mamba/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?message=$1 [L]
The first line turns on the rewrite engine for the /mamba/ directory. Without doing this, the rules won't get applied (and the rules in the parent directory gets applied instead). The RewriteBase tells the rules here that any relative URI in the target should have /mamba/ as a base URI. The 2 conditions say that the request must not point to an existing file or directory, and the rule rewrites the request and puts it in the message query string parameter for index.php.

Allow only direct access on a specific folder name via .htaccess

I need to allow direct access on a specific folder name (and subcontents) via .htaccess, and deny all other.
To start, my current .htaccess is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
My file structure is, for instance:
modules/
core/
_test/
publics/
file.txt
other/
_whatever/
publics/
more.txt
publics/
sub/
file.txt
other.txt
.htaccess
I could not access anything, except by files and folders that are in publics folder. On this example, I could access, without problem:
modules/core/publics/ [dir]
modules/core/publics/file.txt
modules/other/_whatever/publics/ [dir]
modules/other/_whatever/publics/more.txt
modules/other/publics/ [dir]
modules/other/publics/sub/ [dir]
modules/other/publics/sub/file.txt
modules/other/publics/other.txt
I could not have access to any file or folder.
I guess that we will need regular expression, but I don't know how I can use this on mod_rewrite to works like I want.
Some suggestion?
A slight modification to your current htaccess. Any access not under a publics folder will be redirected to index.php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !publics/
RewriteRule . index.php [L]

Use htaccess to rewrite subdomain files to a folder

I need to rewrite a files on subdomain to files stored in the folder with name of the subdomain. Only files that start with sitemap should be rewritten. So, if the requests look like this:
http://demo.domain.com/sitemap.xml
http://demo.domain.com/sitemap_more.xml
http://test.domain.com/sitemap.xml
http://test.domain.com/sitemap_alpha.xml
These should rewrite to files:
/content/sitemaps/demo/sitemap.xml
/content/sitemaps/demo/sitemap_more.xml
/content/sitemaps/test/sitemap.xml
/content/sitemaps/test/sitemap_alpha.xml
So, subdomain name is used as a folder in the rewrite-to-path. This should be rewriting NOT redirecting. Also, it would be good to have rules that will work with any domain without need to change domain name everytime.
You can match the subdomain with a condition, then rewrite it with the rule :
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/content/sitemaps/%1/$1 [L]
Maybe you want to add a filter on .xml
Add :
RewriteCond %{REQUEST_FILENAME} \.xml
Edit
For file beginning with sitemap :
RewriteCond %{REQUEST_FILENAME} sitemap.*\..+$
This will match any string that finish with sitemap. and end. => this is a filename not an inner match of directory.

Storing domain files in subfolder

I'm often updating my website through various design iterations, and want to simplify my life by putting each version in its own folder: ie: www.mysite.com/v1.
How can I store all the contents of my in that folder (/v1, /v2, etc) yet have it accessed by simply typing in www.mysite.com.
I don't want just want to redirect the url, I want to remove the v1 from the url entirely.
Assuming you're using Apache, you could use mod_rewrite for this. Simply create a .htaccess file in the root of your public directory with a simple rewrite rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) v2/$1 [L]
</IfModule>
This will rewrite all URL's to the v2 directory. If you update your site to a new version, simply change v2 into something else and all requests will be rewritten to that directory.

Resources