使用的.htaccess重写通过ID和文件名网址 [英] Using .htaccess to Rewrite URLs with ID and Filename

查看:214
本文介绍了使用的.htaccess重写通过ID和文件名网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

现在的问题是,我想了文章是一个变量,例如,它可以被更改为并会直接向类似: http://mysite.com/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: http://mysite.com/article.php?id=1.

I would like to have a URL like this direct there: http://mysite.com/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: http://mysite.com/page.php?id=1, from http://mysite.com/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和文件名网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆