dozabt4329 2014-11-26 17: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 17: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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办