简体   繁体   中英

mod_rewrite to direct site.com/something to index.php?values=something

(I might be an idiot, it appears that .htaccess isn't enabled by default in Apache on OSX.)

I know there are a lot of mod_rewrite questions out there, but I just can't get mine right and nothing else seems to quite cover it.

All I want to do is redirect

www.url.com/something

or

www.url.com/something/anything/more

to

www.url.com/index.php?values=something

or

www.url.com/index.php?values=something/anything/more

Nothing fancier than that (I'll put in something about ignoring gif/jpg/png, adding www. etc later later) - all processing of something/anything/more will be done in PHP.

I just can't get it right though. Any hints/resources or anyone who's just managed to do this? It's more frustrating knowing that I once got it absolutely perfect but lost that code years ago.

RewriteEngineOn
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d    
RewriteRule ^(.*)$ /index.php?values=$1 [L]

Please note that you need to have AllowOverride On set in the Apache host config file for the directory in question if you are trying to do this in an .htaccess file and of course need to have mod_rewrite module enabled.

This rule will ignore the redirect for any actual files or directories (ie images) that actually exist (including index.php).

This is pretty straightforward, at least in what you are asking But basically you need to make index.php passthrough (so you don't rewrite index.php to index.php), then the main rewrite.

RewriteEngine On
RewriteRule index.php - [L]

# your conditions here
RewriteRule ^/?(.*)$ /index.php?values=$1 [L]

Add whatever conditions you want to exclude rewrite at the # your conditions here spot

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