Emre
Emre

Reputation: 31

How to create permalink in htaccess

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

Upvotes: 3

Views: 12192

Answers (3)

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]

Upvotes: 1

LazyOne
LazyOne

Reputation: 165471

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/

Upvotes: 6

Alan B. Dee
Alan B. Dee

Reputation: 5616

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

Upvotes: 0

Related Questions