使用的.htaccess重写通过ID和文件名网址

编程入门 行业动态 更新时间:2024-10-22 19:48:10
本文介绍了使用的.htaccess重写通过ID和文件名网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想重写.htaccess的一个URL,不得不说我无法找到答案,很多小时的搜索后一个问题。而且,它并不能帮助我的正则表达式的技能不是很大......

我的网址的基本格式是这样的: mysite/article.php?id=1

我想有这样直接的有一个网址: mysite/article/1/made-up-irrelevant-title-slug 。

现在的问题是,我想了文章是一个变量,例如,它可以被更改为页并会直接向类似: mysite/page.php?id=1 ,从的http:// mysite的。 COM /页/ 1 /虚构的页面名称。

如果有人知道如何正确地写,我将非常感谢!

解决方案

#打开修改可以 RewriteEngine叙述上 #不要重写现有的文件,目录或符号链接。 的RewriteCond%{} REQUEST_FILENAME -s [OR] 的RewriteCond%{} REQUEST_FILENAME -l [OR] 的RewriteCond%{} REQUEST_FILENAME -d 。重写规则^ * $ - [NC,L] #重写/组件/ ID /标题 - > COMPONENT.php?ID = ID 重写规则^([^ /] +)/(\ d +)/.*$ $ 1.PHP?ID = $ 2 [L]

I am trying to rewrite a URL with .htaccess and had a question that I'm unable to find an answer to after many hours of searching. And, it doesn't help that my regex skills aren't that great...

The underlying format for my URLs is this: mysite/article.php?id=1.

I would like to have a URL like this direct there: mysite/article/1/made-up-irrelevant-title-slug.

The problem is, I'd like the article to be a variable such that it could be changed to page and would direct to something like: mysite/page.php?id=1, from mysite/page/1/made-up-page-name.

If anyone knows how this is properly written, I would be extremely grateful!

解决方案

# Turn on Rewrites RewriteEngine on # Do not rewrite existing files, directories, or symlinks. RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] # Rewrite /COMPONENT/ID/TITLE -> COMPONENT.php?id=ID RewriteRule ^([^/]+)/(\d+)/.*$ $1.php?id=$2 [L]

更多推荐

使用的.htaccess重写通过ID和文件名网址

本文发布于:2023-10-31 18:58:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546985.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   文件名   网址   htaccess   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!