【问题标题】:how to shorten url using htaccess如何使用 htaccess 缩短 url
【发布时间】:2013-06-22 00:43:13
【问题描述】:

大家好,我需要使用 htaccess 缩短我的网址 喜欢

之前

www.mysite.com/obj/task/profile/id/1568/用户名

之后

www.mysite.com/1568/用户名

或

之前

www.mysite.com/obj/task/page/city/41280/pageId/22/clubs//Test%20town

之后

www.mysite.com/41280/22/clubs//Test%20town

我如何使用 htaccess 实现这一点

我的 htaccess 文件有这个

RewriteEngine on
Options +FollowSymlinks
Options -Indexes

RewriteCond %{REQUEST_URI} (.+)$  [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
AddDefaultCharset utf-8
RewriteRule (.*) index.php 

RewriteEngine on
RewriteCond %{HTTP_HOST} ^test.com [NC]
RewriteRule ^(.*)$ http://www.test.com/$1 [L,R=301]  

建议更改

<files .htaccess>
    order allow,deny
    deny from all
</files>
php_value memory_limit 170M

RewriteEngine on
Options +FollowSymlinks
Options -Indexes

RewriteCond %{REQUEST_URI} (.+)$  [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
AddDefaultCharset utf-8
RewriteRule (.*) index.php


#RewriteCond %{HTTPS} !=on
#RewriteCond %{HTTP_HOST} !^$ [NC] 
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

# THIS  RULE FOR FOR INACTIVE HTTPS
#RewriteCond %{HTTP_HOST} !^www.*$ [NC]
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 


RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]  

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

# from /obj/task/profile/id/1568/username
# to /1568/username
RewriteRule ^obj/task/profile/id(/.+)$/$1 [R=302,L,NC]

# from obj/task/page/city/41280/pageId/22/clubs//Test%20town
# to 41280/22/clubs//Test%20town
RewriteRule ^obj/task/page/city/([^/]+)/pageId(/.+?)/?$ /$1/$2 [R=302,L,NC,NE]

我访问过 www.mysite.com/obj/task/profile/id/1568/username 链接仍然没有缩短。

【问题讨论】:

  • /obj/task/profile/id OR /obj/task/page/city/ 是静态文本吗?
  • 什么是静态文本?此外,当用户登录并访问他自己的个人资料时,网址是 www.mysite.com/dtf/task/profile。
  • 您希望两者都显示相同的页面吗?或者将访问较长版本的用户重定向到较短的版本?或者将访问较短的用户重定向到较长的用户?
  • Shorten Url using htaccess 的可能重复项
  • 我希望用户被重定向到更短的版本

标签: .htaccess url


【解决方案1】:

通过httpd.conf启用mod_rewrite和.htaccess,然后把这段代码放到你.htaccess的DOCUMENT_ROOT目录下:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# from /obj/task/profile/id/1568/username
# to /1568/username
RewriteRule ^obj/task/profile/id(/.+)$ /$1 [R=302,L,NC]

# from obj/task/page/city/41280/pageId/22/clubs//Test%20town
# to 41280/22/clubs//Test%20town
RewriteRule ^obj/task/page/city/([^/]+)/pageId(/.+?)/?$ /$1/$2 [R=302,L,NC,NE]

【讨论】:

  • 网址仍然很长.. :(
  • 如果您想在浏览器中更改 URL,还请添加 R 标志。查看我的编辑。
  • 感谢@anubhava 的帮助。我已经尝试了您给出的新命令,但仍然无法正常工作。网址仍然很长
  • 好的,请在您的问题中提供您最新的 .htaccess(以及我建议的更改),并告诉我您用于测试的 URI 是什么?
  • 刚刚编辑了我的问题。我试图访问以下链接 www.mysite.com/obj/task/profile/id/1568/username 它显示页面但是当我访问 www.mysite.com/1568/username 时它什么也没显示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-10
  • 2012-03-27
  • 1970-01-01
相关资源
最近更新 更多