2010-08-27 2 views
0

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

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

Однако, когда я пытаюсь этот код, я просто получаю 404 говорит мне, что/страница/дома не существует.

Options +FollowSymLinks 

RewriteEngine on 

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

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

ответ

1

Вашего шаблон не соответствует вашему примеру 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 
+0

Это прекрасно работает. Я думаю, что ([^ /] +) /? $ Исправляет его. Не могли бы вы объяснить части этого выражения, чтобы я мог лучше понять это, пожалуйста? – kaotix

+0

@kaotix - Я добавил комментарии к правилу сейчас. Я не был уверен, насколько вы знакомы с регулярными выражениями в целом, поэтому я попытался объяснить как можно подробнее. –

2

Вашего правило перезаписи использует индекс/P/ххххй, но вы хотите/страница/ххое

попробовать

RewriteRule ^/page/(.*)/ index.php?p=$1 
RewriteRule ^/page/(.*) index.php?p=$1 
+0

Это то, что я должен был разместить в своем оригинальном посте. Извините за путаницу. – kaotix