【问题标题】:.htaccess url: rewrite is not working on local host.htaccess url:重写在本地主机上不起作用
【发布时间】:2014-09-23 08:15:06
【问题描述】:

我是 .htaccess 的新手 我当前的网址是:

 http://localhost/www.zeeshan.com/details.php?p_id=999

如果我想这样显示我的网址,它对我有好处。

http://localhost/www.zeeshan.com/details/999

我的 .htaccess 代码是

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /www.zeeshan.com/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^details/([^/]+)$ details.php?p_id=$1  [NC,L]

我的问题是当使用此链接时我无法获得正确答案:

 http://localhost/www.zeeshan.com/details.php?p_id=999&p_title=nokia%20mobile%20for%20sale

我使用了这个 .htaccees 代码

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /www.zeeshan.com/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^details/(\d+)-([\w-]+)$ details.php?p_id=$1&p_title=$2 [NC,L]

使用此链接后,我想要这样

http://localhost/www.zeeshan.com/details/999/nokia-mobile-for-sale

我在给定的链接中阅读了很多次这个主题来解决我的问题,但未能得到正确的输出。每次都得到找不到页面错误Rewriting URL with .htaccess local in XAMPP

请帮我解决我的问题。

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    您的正则表达式实际上不正确。使用这条规则:

    Options +FollowSymLinks -MultiViews
    RewriteEngine On
    RewriteBase /www.zeeshan.com/
    
    ## convert spaces to hyphens
    # executes **repeatedly** as long as there are more than 1 spaces in URI
    RewriteRule "^(\S*) +(\S* .*)$" $1-$2 [L,NE]
    
    # executes when there is exactly 1 space in URI
    RewriteRule "^(\S*) (\S*)$" $1-$2 [L,R=302,NE]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^details/(\d+)/([^/]+)/?$ details.php?p_id=$1&p_title=$2 [NC,L,QSA]
    

    【讨论】:

    • 先生,如果我有 2 个页面,例如 index.php 和 detail.php。如果用户以错误的方式打开 detail.php 页面,例如 deatilss.php 我的 .htaccess :url 重定向并打开我自己设计的 404.php 页面?使用上面的 .htaccess 代码
    • yes 404.php page in this i design page not found 错误。
    • 我没有得到你。 404 是预期的结果,并且正在发生。我将离线 2 小时。您可以打开一个新问题以从 SO 社区获得帮助。
    • 我目前在手机上。您能否为这个新问题创建一个新问题,我会在我重新上线后参加。
    猜你喜欢
    • 2014-10-31
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多