我在.htaccess文件中有一些问题

时间:2015-01-11 04:55:28

标签: .htaccess

我在.htaccess文件中有一些问题。我已将我的索引页面托管到webserver。我不知道.htaccess文件中我做错了什么。在.htaccess文件中添加代码后,我的网址仍然不友好。它显示http://rang.comli.com/servicecnt它应该是http://rang.comli.com/servicecnt/service_id/5

<html>
    <head>
        <title>Second mod_rewrite example</title>
    </head>
    <body>
        <p>
            welcome index page. Index page is loaded
            <a  href="http://rang.comli.com/servicecnt.php?service_id=5">Done with php my admin</a>
        </p>
    </body>
</html>

这是我的.htaccess文件:

Options +FollowSymLinks -MultiViews

# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /public_html/index.php to /dir/index
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /public_html/index to /dir/index.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteEngine On
RewriteCond %{QUERY_STRING} ^service_id=([0-9]*)$
RewriteRule ^servicecnt\.php$ /servicecnt.php/%1? [R=302,L]

2 个答案:

答案 0 :(得分:0)

您可以使用:

Options +FollowSymLinks -MultiViews

# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} \s/+servicecnt\.php\?service_id=([^\s&]+) [NC]
RewriteRule ^ servicecnt/service_id/%1? [R=302,L]

## hide .php extension
# To externally redirect /public_html/index.php to /dir/index
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

RewriteRule ^servicecnt/service_id/(\d+)/?$ servicecnt.php?service_id=$1 [QSA,L]

## To internally redirect /public_html/index to /dir/index.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

答案 1 :(得分:0)

你最好的选择不是重定向旧网址,而是让php首先生成漂亮的网址。每次用户点击您的链接时,服务器都需要重定向用户,这是非常低效的。如果用户改为单击新的URL,则只需在内部重写它。您可以使用以下规则重写它:

RewriteRule ^servicecnt/service_id/([^/]+)/?$ servicecnt.php?service_id=$1 [L]