2

我正在尝试使用 php 制作 SEO 友好的网站...

在文件夹 jcheck 我有一个 index.php 文件,它以一个参数“id”作为输入。

mydomain.com/jcheck/index.php?id=1 works

我怎样才能使它如下

mydomain.com/jcheck/1

我试过制作 .htaccess 文件并把

RewriteEngine on

RewriteCond %{REQUEST_URI} 1/
RewriteRule 1/ http://mydomain.com/jcheck/index.php?id=1

我怎样才能让它工作?

4

5 回答 5

4

您基本上可以通过两种方式做到这一点:

带有 mod_rewrite 的 .htaccess 路由

在根文件夹中添加一个名为 .htaccess 的文件,然后添加如下内容:

RewriteEngine on
RewriteRule ^/Some-text-goes-here/([0-9]+)$ /picture.php?id=$1

这将告诉 Apache 为该文件夹启用 mod_rewrite,如果它被询问与正则表达式匹配的 URL,它会在内部将其重写为您想要的内容,而最终用户不会看到它。简单但不灵活,因此如果您需要更多功能:

PHP 路线

Put the following in your .htaccess instead:

FallbackResource index.php

This will tell it to run your index.php for all files it cannot normally find in your site. In there you can then for example:

$path = ltrim($_SERVER['REQUEST_URI'], '/');    // Trim leading slash(es)
$elements = explode('/', $path);                // Split path on slashes
if(count($elements) == 0)                       // No path elements means home
    ShowHomepage();
else switch(array_shift($elements))             // Pop off first item and switch
{
    case 'Some-text-goes-here':
        ShowPicture($elements); // passes rest of parameters to internal function
        break;
    case 'more':
        ...
    default:
        header('HTTP/1.1 404 Not Found');
        Show404Error();
}

This is how big sites and CMS-systems do it, because it allows far more flexibility in parsing URLs, config and database dependent URLs etc. For sporadic usage the hardcoded rewrite rules in .htaccess will do fine though.

*****Copy this content from URL rewriting with PHP**

于 2013-09-16T07:36:03.160 回答
2

In the htaccess file in your jcheck directory, use these rules:

RewriteEngine On
RewriteBase /jcheck/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ index.php?id=$1 [L,QSA]
于 2013-09-16T07:58:42.280 回答
1

试试这个:

RewriteRule ^jcheck/([0-9]+)$ jcheck/index.php?id=$1
于 2013-09-16T07:30:03.230 回答
0
RewriteEngine on
RewriteRule ^/jcheck/([0-9]+)$ /jcheck/index.php?id=1

也可以看看这里它对你有用

于 2013-09-16T07:29:55.610 回答
0

将 .htaccess 文件放入文件jcheck夹并写入:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-D
RewriteRule ^1/?$ index.php?id=1
于 2013-09-16T07:34:37.740 回答