переменные mod_rewrite и php - PullRequest
       13

переменные mod_rewrite и php

0 голосов
/ 27 августа 2010

Я пытаюсь заставить mod_rewrite работать с моим сайтом, но по какой-то причине он не работает.Я уже ввел код в свой файл .htaccess, чтобы перенаправить не-www на www, поэтому я знаю, что mod_rewrite работает в целом.

URL-адрес, который я пытаюсь изменить, - example.com/index.php?p=home, поэтому новый URL будетbe example.com/page/home

Однако, когда я пробую этот код, я просто получаю 404, сообщая мне, что / page / home не существует.

Options +FollowSymLinks

RewriteEngine on

RewriteRule index/p/(.*)/ index.php?p=$1
RewriteRule index/p/(.*) index.php?p=$1

Может кто-нибудь помочь мне, пожалуйста?

Ответы [ 2 ]

2 голосов
/ 27 августа 2010

Ваше правило перезаписи использует index / p / xxxxx, но вы хотите / page / xxxx

try

RewriteRule ^/page/(.*)/ index.php?p=$1
RewriteRule ^/page/(.*) index.php?p=$1
1 голос
/ 27 августа 2010

Ваш шаблон не соответствует вашему примеру URL.Предполагая, что ваш пример URL был правильным, вы хотели вместо этого:

Options +FollowSymLinks

RewriteEngine on

# We want to rewrite requests to "/page/name" (with an optional trailing slash)
# to "index.php?p=name"
#
# The input to the RewriteRule does not have a leading slash, so the beginning
# of the input must start with "page/". We check that with "^page/", which
# anchors the test for "page/" at the beginning of the string.
#
# After "page/", we need to capture "name", which will be stored in the
# backreference $1. "name" could be anything, but we know it won't have a
# forward slash in it, so check for any character other than a forward slash
# with the negated character class "[^/]", and make sure that there is at least
# one such character with "+". Capture that as a backreference with the
# parenthesis.
#
# Finally, there may or may not be a trailing slash at the end of the input, so
# check if there are zero or one slashes with "/?", and make sure that's the end
# of the pattern with the anchor "$"
#
# Rewrite the input to index.php?p=$1, where $1 gets replaced with the
# backreference from the input test pattern
RewriteRule ^page/([^/]+)/?$ index.php?p=$1
...