简体   繁体   中英

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.

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.

To further complicate things, there is a .htaccess file in the base directory (ie the parent directory of subdir), which has the following code:

<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/...

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

Will be rewritten to: /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']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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