dozabt4329 2014-11-26 09:06
浏览 55
已采纳

.htaccess - 用正斜杠替换连字符(破折号)' - ''

I'm having a problem with .htaccess

At this time I have something like this:

http://example.com/page-example?getVariable=1234

But I want something like:

http://example.com/page/example/1234

I tried to use that .htacces to replace the hyphens with slashes

Options +FollowSymlinks -MultiViews
RewriteRule ^([^/]+)-([^/]+).php/?$ /$1/$2 [R,L]

And I got something like this

 http://example.com/page/example?getVariable=1234

But the server can't find the path, I think it's because it admits that the 'page' is a folder.

How can I do what I want to, but the server search for the file 'page-example.php' at the root folder and not for 'example.php' at '/page/' ?

PS: I'm using already the code below to remove the .php extension and the 'www.' from the URL.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
  • 写回答

1条回答 默认 最新

  • douaonong7807 2014-11-26 09:27
    关注

    Before the rules that you already have, try adding:

    RewriteCond %{THE_REQUEST} \ /+page-([^/]+)(?:\.php|)\?getVariable=([^&\ ]+)
    RewriteRule ^ /page/%1/%2? [L,R]
    
    RewriteRule ^page/([^/]+)/([^/]+)$ /page-$1.php?getVariable=$2 [L,QSA]
    

    So that the whole thing looks like:

    RewriteEngine on
    
    RewriteCond %{THE_REQUEST} \ /+page-([^/]+)(?:\.php|)\?getVariable=([^&\ ]+)
    RewriteRule ^ /page/%1/%2? [L,R]
    
    RewriteRule ^page/([^/]+)/([^/]+)$ /page-$1.php?getVariable=$2 [L,QSA]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部