简体   繁体   English

如何重写URL?

[英]How to do an URL rewrite?

I don't know anything about rewriting URLs in a .htaccess file and can't seem to figure out how to do what I need to do. 我对在.htaccess文件中重写URL一无所知,而且似乎无法弄清楚该怎么做。

I have URLs like this: 我有这样的网址:

www.website.com/subdir/10-this-is-a-post

I have a single php file in subdir, index.php, and the code inside that file needs to pull content from the datebase based on the number at the beginning of the page name -- in this case, 10. 我在子目录index.php中有一个php文件,该文件中的代码需要根据页面名称开头的数字从日期库中提取内容-在本例中为10。

To further complicate things, there is a .htaccess file in the base directory (ie the parent directory of subdir), which has the following code: 更复杂的是,基本目录(即subdir的父目录)中有一个.htaccess文件,该文件具有以下代码:

<IfModule mod_rewrite.c>  
Options -MultiViews  
RewriteEngine On  
RewriteBase /  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule . /index.php [L]  
</IfModule>  

Any advice on how I can accomplish this? 关于如何实现此目标的任何建议?

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

This will place everything after the /... to index.php/... 这会将所有内容放在/ ...之后到index.php / ...

Example: /subdir/10-this-is-a-post 示例:/ subdir / 10-this-is-a-post

Will be rewritten to: /index.php/subdir/10-this-is-a-post 将被重写为:/index.php/subdir/10-this-is-a-post

Use this to parse the segments: 使用它来解析段:

$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = explode('/', $_SERVER['REQUEST_URI_PATH']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM