Beginner Apache URL Rewrite Question

别来无恙 提交于 2020-01-02 08:56:11

问题


I'm just trying to figure out if I'm on the right path - additional details on rewriting the URL in my example would be appreciated.

I have installed a CMS program and would simply like that www.example.com be pointed to www.example.com/cms. I just want to know if URL rewriting through apache is the best way to accomplish this?

Thank you.


回答1:


Just redirecting http://example.com/ to http://example.com/cms/:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewritRule ^/?$ /cms/
</IfModule>

Redirecting all urls which otherwise would've 404d to start with /cms/:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ /cms/$1 [L]
</IfModule>

Redirecting all urls to /cms/:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^(.*)$ /cms/$1 [L]
  <Directory /var/www/html/cms/> #change this to the correct path
    RewriteEngine Off
  </Directory>
</IfModule>



回答2:


That's definitely the approach I would take. I'm going to assume you're using Apache, though this can easily be done with IIS as well. You'll need to edit your .htaccess file in the root directory to do this using mod_rewrite.

<IfModule mod_rewrite.c>

   RewriteEngine on

   RewriteRule    ^(.*)$ /cms/$1  [L]

</IfModule>

This should work for what you're after. Change "cms" to whatever directory you want to rewrite to.



来源:https://stackoverflow.com/questions/878186/beginner-apache-url-rewrite-question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!