rewrite my url using htaccess
I am working in codeigniter project
I want to rewrite my URL using htaccess. I have a URL like abc.com/main/demo
and I want to change it to abc.com/demo
I just remove the " main " .
how can I achieve开发者_StackOverflowd it .
Sorry for bad English :(
**This should do it:
RewriteEngine On
RewriteBase /
RewriteRule ^main/(.*)$ /$1 [R]
this will do the trick, or you can go to www.tossdown.com for more details**
Actually, on second thought this is probably what you're asking for:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /main
RewriteRule ^(/)?$ index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
You do not need mod_rewrite for simple redirects.
The apache instructions Redirect or RedirectMatch should be sufficient for you:
RedirectMatch 302 ^/main/(.*)$ https://my.website.com/$1
Here I use a 302 redirect, so that it's just a temporary redirect, when your website will be ok (and that redirection working), you should alter it in 301 so that it becomes permanent.
Now what we're all doing in our answer is how to tell your web HTTP clients to use the /demo url instead of /main/demo. But you're complaining about 404... well if you do not have a working index.php or index.html in the root /demo directory it's 'normal'.
So I guess your question is not only how to redirect the user in another directory, but how to make your /var/www/mysite/main/demo
directory (or something like that), with all the files in this directory, responding in the /demo/* url and not /main/demo/*. In this configuration we can guess /var/www/mysite/
is your DocumentRoot. i.e. this is the root for your web server. The simpliest solution is to change this root by altering the DocumentRoot to /var/www/mysite/main
. You could do that in the main configuration of your VirtualHost, maybe you could create a new VirtualHost for your project as well.
If you do not have access to apache main configuration but only to .htaccess and if you run several other websites in some others url we may find more complex solutions (with mod_rewrite redirecting physical directory path based on the website name for example). But you'll have to give us more informations if you want something more.
This should do it:
RewriteEngine On
RewriteBase /
RewriteRule ^main/(.*)$ /$1 [R]
I hope this is not the thousandth answer...
RewriteEngine On
RewriteRule ^demo/$ /main/demo/
精彩评论