开发者

Mod_rewrite trouble: Want to direct from ?= to a flat link, nothing seems to work

开发者 https://www.devze.com 2023-02-05 17:47 出处:网络
I have a site that currently serves results as example.com/index.php?show=foo and I\'d like it to read example.com/show/foo.

I have a site that currently serves results as example.com/index.php?show=foo and I'd like it to read example.com/show/foo.

My understanding is this would make them visible to search engine robots, and it seems a much simpler way to do th开发者_Python百科is than to create a couple hundred html files...

I've tried the following .htaccess code:

Options +FollowSymLinks

RewriteEngine on

RewriteRule ^show/(.*)$ index.php?show=$1 [NC,L]

No dice.

Also tried this, which I found on another stack overflow question:

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([0-9A-Za-z]+)/?$ /index.php?show=$1 [L]

</IfModule>

Any ideas on what I'm missing here?


Note that ^([0-9A-Za-z]+)/?$ will try to match a request URI that only includes alphanumeric characters optionally trailed by a slash. Therefore the URI show/foo will not be matched because there are more characters at the end (ie, after the slash, where the expression expects to find the end of the string).

Try:

RewriteRule ^show/([0-9A-Za-z]+)/?$ /index.php?show=$1 [L]

Also, to capture aditional query parameters, you could do:

RewriteRule ^show/([0-9A-Za-z]+)/?$ /index.php?show=$1&%{QUERY_STRING} [L]

This means a URL like /show/page?id=1 rewrites to /index.php?show=page&id=1


I do something like this on sites that use 'seo-friendly' URLs.

In .htaccess:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L]

Then on index.php:

if ($_SERVER['REQUEST_URI']=="home") {
    include ("home.php");
}

The .htaccess rule tells it to load index.php if the file or directory asked for was not found. Then you just parse the request URI to decide what index.php should do.

You're on the right track, but your rule has issues. .* is pretty all inclusive. Start there and go more restrictive if needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消