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

报告相同问题?

悬赏问题

  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符