开发者社区 问答 正文

PHP中使用.htaccess的漂亮URL

我有一个链接,这是

  1. http://musicfoorti.com/categorylist/57/0/1.html

假设http://musicfoorti.com/categorylist/61/0/1.html这是宝莱坞电影歌曲类别,我想将其重写为http://musicfoorti.com/bollywood-movie-songs.html

这意味着每个类别链接都将类似于http://musicfoorti.com/category-name.html ,这是我的.htaccess新代码,.htaccess所以我不能这样做。选项-MultiViews +关注符号链接-索引

SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule ^categorylist/([0-9]+)/([0-9]+)/([0-9]+)\.html$ /index.php?pid=$1&sort=$2&page=$3 [L]

RewriteRule ^fileDownload/([0-9]+)/(.*)\.html$ /file.php?fid=$1 [L]

RewriteRule ^download/([0-9]+).html$ /file.php?fid=$1&download [L]

RewriteRule ^([0-9]+)/updates\.html$ /updates.php?page=$1 [L]

RewriteRule ^search\.html$ /search.php [L]

RewriteRule ^index\.html$ /index.php [L]

<IfModule mod_env.c>
SetEnv SEO_SUPPORT 1
</IfModule>
</IfModule>

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE application/javascript text/css text/html text/xml
</IfModule>

这是我的PHP代码

                 <div>
                    <h3><a href="'.$sabb->settings['url'].'/categorylist/'.$file['fid'].'/'.$sort.'/1.html">'.escape($file['name']).'</a></h3>
                    <div class="listbox-tags">'.sprintf($_admin, $file['fid'], $file['fid']).'</div>
                </div>                    

请告诉我该怎么办?

展开
收起
社区秘书 2019-12-04 13:49:36 748 分享 版权
阿里云 AI 助理回答

要实现您所描述的URL重写,即把http://musicfoorti.com/categorylist/61/0/1.html这样的链接重写为更友好的http://musicfoorti.com/bollywood-movie-songs.html形式,您需要在.htaccess文件中添加一条新的重写规则来匹配特定的ID并映射到相应的友好名称。由于直接将每个ID硬编码到规则中是不实际的,通常的做法是建立一个映射关系,比如通过数据库查询或者在代码中定义这些映射。

但是,基于您的需求,我可以提供一个简化的示例,假设您只有几个固定的类别需要重写,并且可以手动配置这些映射。以下是一个基本的示例,展示了如何针对特定ID进行重定向:

RewriteEngine On

# 为特定的category ID设置重定向
RewriteCond %{REQUEST_URI} ^/categorylist/61/0/1\.html$
RewriteRule ^ /bollywood-movie-songs.html [L,R=301]

# 如果有更多类别,继续添加类似规则
# RewriteCond %{REQUEST_URI} ^/categorylist/另一ID/0/1\.html$
# RewriteRule ^ /another-category-name.html [L,R=301]

这段代码会检查请求的URI是否匹配/categorylist/61/0/1.html,如果是,则将其永久重定向(301)到/bollywood-movie-songs.html

然而,如果您的类别数量很多或经常变化,手动维护这些重写规则将变得非常不便。在这种情况下,建议在后端应用(如PHP、Python等)中处理这种逻辑,根据类别ID查询对应的友好名称,然后进行内部路由或重定向。这样可以更加灵活和可扩展。

请注意,上述方法仅适用于静态映射的情况。对于动态生成的类别页面,您可能需要结合后端语言和数据库查询来实现更复杂的逻辑。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答