简体   繁体   English

使用.htaccess / mod_rewrite缩短URL?

[英]Shortening URLs using .htaccess / mod_rewrite?

How would I go about shortening down my URLs? 我该如何缩短我的网址? At present my URLs are like http://www.server.com/username/aa/index.php/site/products and I'd prefer the index.php part not to be there if possible. 目前,我的URL类似于http://www.server.com/username/aa/index.php/site/products并且我希望index.php部分尽可能不存在。

In general you can a regular expression to fish out the stuff you want to keep and then concatenate it into a replacement. 通常,您可以使用正则表达式将要保留的内容提取出来,然后将其连接成替换内容。 In the snippet bellow ^(.*) (1 or more of any character from the beginning till we hit 'index.php') becomes $1 and eveything after index.php $2 在摘要中,^(。*)(从开始到我们击中“ index.php”为止,任何字符中的1个或多个)都变为$ 1,并且在index.php $ 2之后发出

RewriteRule ^(.*)/index\.php/(.*)$ $1/$2[L]

As far as codeingniter and other frameworks go, this should not be necessary at all, you should have something like this in the public directory of your project: 就codeingniter和其他框架而言,这根本没有必要,您应该在项目的公共目录中包含以下内容:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

This willfetch anything going to this directory and redirect to index.php unless a file or directory or link with the explicit name exists 除非存在明确名称的文件或目录或链接,否则它将获取进入该目录的所有内容并重定向到index.php。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM