Rewrite rule for redirect url

左心房为你撑大大i 提交于 2019-12-12 03:53:08

问题


I want to apply rewrite rule for achieve this.

current url : http://localhost/mysite/?userlogin

rewrite url : http://localhost/mysite/userlogin

I want to do like when any user open http://localhost/mysite/userlogin this url, at that time user must get view of http://localhost/mysite/?userlogin this url.

[Note] : current url is working right now..But I m getting Object Not found on rewrite url.

Thanks in advance.


回答1:


If you are using an Apache server

You can do this creating a .htaccess in your web application root directory with this content:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?p=$1 [QSA]

If you have more than one site on your root folder, like this:
| root
|_ web1
|_ web2
|_ web3

you need to add a .htaccess file only to the folder that you want to enable rewrite rules, example:
| root
|_ web1 |__ .htaccess
|_ web2
|_ web3

Note: To use the above solution, you need to enable .htaccess module in Apache2.
In your apache2>sites-available>default or apache2>sites-available>your_config, set AllowOverride to 'All':

<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>

Note: (Edit the code above according to your existing code.)

If you are using a Nginx server

You need to add these lines to your nginx server config!

if (!-f $request_filename){
    set $rule_0 1$rule_0;
}
if (!-d $request_filename){
    set $rule_0 2$rule_0;
}
if ($rule_0 = "21"){
    rewrite /(.*) /index.php?p=$1;
}

If you are using a Windows IIS Server

You need to add a web.config with these rules (I suggest to use the URLRewrite 2.0 in IIS Control panel to add these l

<rule name="rule 1Z">
    <match url="(.*)"  />
    <action type="Rewrite" url="/index.php?p={R:1}"  appendQueryString="true" />
</rule>


来源:https://stackoverflow.com/questions/35646879/rewrite-rule-for-redirect-url

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