简体   繁体   English

使用htaccess重写我的网址

[英]rewrite my url using htaccess

I am working in codeigniter project I want to rewrite my URL using htaccess. 我在codeigniter项目中工作,我想使用htaccess重写我的URL。 I have a URL like abc.com/main/demo and I want to change it to abc.com/demo I just remove the " main " . 我有一个类似abc.com/main/demo的URL,我想将其更改为abc.com/demo我只是删除了“ main”。 how can I achieved it . 我怎么能做到这一点。

Sorry for bad English :( 对不起,英语不好:(

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. 对于简单的重定向,您不需要mod_rewrite。

The apache instructions Redirect or RedirectMatch should be sufficient for you: apache指令Redirect或RedirectMatch应该足以满足您的要求:

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. 在这里,我使用302重定向,因此它只是一个临时重定向,当您的网站正常(并且该重定向正常工作)时,您应该在301中对其进行更改,以使其永久不变。

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. 现在,我们在回答中所要做的就是如何告诉您的Web HTTP客户端使用/ demo url而不是/ 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' . 但是,您抱怨的是404 ...如果在/ demo根目录中没有可用的index.php或index.html,那就很正常了

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/*. 因此,我想您的问题不仅是如何重定向用户到另一个目录中,还包括如何使/var/www/mysite/main/demo目录(或类似名称)与该目录中的所有文件一起响应/ demo / *网址,而不是/ main / demo / *。 In this configuration we can guess /var/www/mysite/ is your DocumentRoot . 在此配置中,我们可以猜测/var/www/mysite/是您的DocumentRoot ie this is the root for your web server. 即,这是您的Web服务器的根目录。 The simpliest solution is to change this root by altering the DocumentRoot to /var/www/mysite/main . 最简单的解决方案是通过将DocumentRoot更改为/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. 您可以在VirtualHost的主要配置中执行此操作,也可以为项目创建一个新的VirtualHost。

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). 如果您无权访问apache主配置,而只能访问.htaccess,并且如果您以其他网址运行其他几个网站,我们可能会找到更复杂的解决方案(例如,mod_rewrite根据网站名称重定向物理目录路径)。 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]

this will do the trick, or you can go to www.tossdown.com for more details** 这可以解决问题,或者您可以访问www.tossdown.com了解更多详细信息**

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/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM