现有网站中的简单 mod_rewrite

发布于 2024-12-11 12:06:47 字数 1014 浏览 0 评论 0原文

我正在做一个网站(针对葡萄牙语国家),一次显示一个短语。

我希望网站有更简单的非动态地址,以便 SEO 更友好并且更容易让用户记住。所以我的问题是这样的,提前感谢所有可能的帮助:

nfrases.com/ -> completely random phrase (don't need changing this!)
nfrases.com/index.php?id_frase=2222 -> phrase with ID (wanted this to be nfrases.com/2222)
nfrases.com/tag.php?tag_nome=amor -> random phrase with tag_nome (want: nfrases.com/amor)
nfrases.com/tag.php?tag_nome=amor&id_frase=2222 -> specific phrase with tag=amor (want: nfrases.com/amor/2222)

我认为这是一个关于 mod_rewrite 的菜鸟问题,但这正是我的问题! :D 已经用我所掌握的少量知识进行了多次尝试,但要么我得到了 404 页,要么 $_GET['tag_nome'] 或 $_GET['id_frase'] 无法接收参数。

我实际拥有的 htaccess 是这样的,但它不起作用:

Options +FollowSymlinks -MultiViews

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.nfrases.com$
RewriteRule ^(.*)$ http://www.nfrases.com/$1 [R=301]

RewriteRule ^([0-9]+)(.*) index.php?id_frase=$1
RewriteRule ^(.*)/([0-9]+) tag.php?tag_nome=$1&id_frase=$2 [L,QSA]

你们能帮我吗?

再次感谢一切!我很感激你的帮助。

I'm doing a website (for portuguese speaking countries) that displays phrases one at a time.

I would like to have simpler non-dynamic addresses for the site to be SEO friendlier and easier to memorize to the user. So my problem is this, and thanks in advance for all the help possible:

nfrases.com/ -> completely random phrase (don't need changing this!)
nfrases.com/index.php?id_frase=2222 -> phrase with ID (wanted this to be nfrases.com/2222)
nfrases.com/tag.php?tag_nome=amor -> random phrase with tag_nome (want: nfrases.com/amor)
nfrases.com/tag.php?tag_nome=amor&id_frase=2222 -> specific phrase with tag=amor (want: nfrases.com/amor/2222)

I think this is a noob question about mod_rewrite but that's exactly what I am! :D Already made several tries with the few knowledge I have but either i got 404 pages either the parameters wouldn't be received by the $_GET['tag_nome'] or $_GET['id_frase'].

The actual htaccess I have is this but it isn't working:

Options +FollowSymlinks -MultiViews

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.nfrases.com$
RewriteRule ^(.*)$ http://www.nfrases.com/$1 [R=301]

RewriteRule ^([0-9]+)(.*) index.php?id_frase=$1
RewriteRule ^(.*)/([0-9]+) tag.php?tag_nome=$1&id_frase=$2 [L,QSA]

Can you guys help me?

Thanks again for everything! I appreciate your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栖迟 2024-12-18 12:06:47

以下重写规则适用于我的 RewriteRule 测试器

#removes trailing slash
#/vida/222/ becomes /vida/222
#/222/ becomes /222
RewriteRule ^(.+)/$ /$1 [R=301]

#/222 silently rewrites to /index.php?id_frase=222
RewriteRule ^([0-9]+)$ index.php?id_frase=$1 [L]

#/vida silently rewrites to /index.php?tag_nome=vida
RewriteRule ^([a-zA-Z-]+)$ index.php?tag_nome=$1 [L]

#/vida/222 silently rewrites to /index.php?tag_nome-vida&id_frase=222
RewriteRule ^([^/]+)/([0-9]+)$ tag.php?tag_nome=$1&id_frase=$2 [L]

The following rewrite rules work for me on a RewriteRule tester:

#removes trailing slash
#/vida/222/ becomes /vida/222
#/222/ becomes /222
RewriteRule ^(.+)/$ /$1 [R=301]

#/222 silently rewrites to /index.php?id_frase=222
RewriteRule ^([0-9]+)$ index.php?id_frase=$1 [L]

#/vida silently rewrites to /index.php?tag_nome=vida
RewriteRule ^([a-zA-Z-]+)$ index.php?tag_nome=$1 [L]

#/vida/222 silently rewrites to /index.php?tag_nome-vida&id_frase=222
RewriteRule ^([^/]+)/([0-9]+)$ tag.php?tag_nome=$1&id_frase=$2 [L]
腻橙味 2024-12-18 12:06:47

您可能想要这样的内容:

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

在这种情况下,nfrases.com/2222 将重写为index.php/2222。这可以轻松地在您的index.php 文件中允许基本路由。对于更高级的路由,使用一个好的框架可能会更快更容易。查看 FuelPHP (http://www.fuelphp.com) 或 CodeIgniter (http://www.codeigniter.com)。

You may want something like this:

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

In this case, nfrases.com/2222 will rewrited as index.php/2222. This easily allows base routing in your index.php file. For more advanced routing, it may be faster and easier to use a good framework. Take a look at FuelPHP (http:www.fuelphp.com) or CodeIgniter (http://www.codeigniter.com).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文