Htaccess rewrite url (Shorten url)

2019-09-07 22:34发布

So I have the url: myurl.com/projects/url/visit.php?link=fbehe and I want to rewrite it as so:

myurl.com/u/fbehe

But it isn't working. I am using this so far:

RewriteEngine On
RewriteRule ^/u/([^/]*)$ visit.php?link=$1 [L]

I would also like to note that I placed my htaccess file in the directory with my visit.php file. so myurl.com/projects/url/.htaccess

How could I achieve this?

标签: .htaccess
3条回答
我命由我不由天
2楼-- · 2019-09-07 22:53

Try using NC as well. Also, not sure if the absolute path is working. Try a relative if the .htaccess is in the root of your website.

RewriteRule ^/u/([^/]*)$ visit.php?link=$1 [L,NC]
查看更多
我命由我不由天
3楼-- · 2019-09-07 23:11

do you use any framework of this? maybe u can you there functionality like routes. can rewrite your url..

Like Codeigniter,Cake

They have routes to change the url like that.

查看更多
一纸荒年 Trace。
4楼-- · 2019-09-07 23:17

You may try this:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}  ^/u/([^/]+)/?$    [NC]
RewriteRule .*       projects/url/visit.php?link=%1 [L]

Maps silently

http://myurl.com/u/anyvalue with or without trailing slash

To

http://myurl.com/projects/url/visit.php?link=anyvalue

查看更多
登录 后发表回答