How to create permalink in htaccess

自闭症网瘾萝莉.ら 提交于 2019-12-06 06:24:58

问题


I want to redirect a link to another with .htaccess file in Linux host. Can you help me?

from: http://example.com/examp
to: http://example.com/examp.php

And another one for my other site

from: http://example.com/examp
to: http://example.com/user.php?u=examp

回答1:


You will need mod_rewrite enabled for this. Start with placing these lines into .htaccess:

RewriteEngine On
RewriteBase /

TBH I'm not 100% sure what do you mean exactly by permalink and how do you want to redirect, so I will provide 2 variants for each URL: rewrite (internal redirect) and redirect (301 Permanent Redirect).

1. This will rewrite (internal redirect) request for http://example.com/examp to http://example.com/examp.php while URL will remain unchanged in browser:

RewriteRule ^examp$ examp.php [L]

2. This will do the same as above but with proper redirect (301 Permanent Redirect) when URL will change in browser:

RewriteRule ^examp$ http://example.com/examp.php [R=301,L]

3. This will rewrite (internal redirect) request for http://example.com/examp to http://example.com/user.php?u=examp while URL will remain unchanged in browser:

RewriteRule ^examp$ user.php?u=examp [QSA,L]

4. This will do the same as above but with proper redirect (301 Permanent Redirect) when URL will change in browser:

RewriteRule ^examp$ http://example.com/user.php?u=examp [QSA,R=301,L]

Useful link: http://httpd.apache.org/docs/current/rewrite/




回答2:


You will need mod_rewrite enabled for this

from: http://example.com/123 to: http://example.com/index.php?q=123

RewriteEngine on
RewriteBase /
RewriteRule ^/?([-A-Za-z0-9]+)/?$ index.php?q=$1 [QSA,L]



回答3:


You'll want to look at RewriteRules and know/understand regular expressions. It'll be something like this:

    RewriteEngine on
    RewriteRule ^(.*)\/examp$ /examp.php [R=301,L]
    - and - 
    RewriteRule ^(.*)\/[a-zA-Z0-9\-\_\.]+$ /user.php?u=$1 [R=301,L]

The latter example will take what's in-between the [] and place it in the $1 variable

Here is a good link to get you started: http://www.webweaver.nu/html-tips/web-redirection.shtml



来源:https://stackoverflow.com/questions/6784302/how-to-create-permalink-in-htaccess

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