1

How can I use .htaccess to rewrite URL for certain pages only?

For example, I want it to work on index.php but not the rest of the pages on the site or on all pages but index.php.

I run in to issues with sub domains and php scripts where the URL redirecting that I am using will mess up stuff. Below is an example of the kind of script I want to use.

Options +FollowSymLinks -MultiViews

# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/([^/]+)/?$ index.php/?node=$1&id=$2 [L,QSA]

RewriteRule ^([^/]+)/?$ index.php/?node=$1 [L,QSA]

Can anyone point me in the right direction?

4

2 回答 2

3

您可以将 URL 作为 REQUEST_URI 服务器变量传递:

RewriteCond %{REQUEST_URI} ^(your_url)$
RewriteRule %1 do_some_stuff

举个例子:

RewriteCond %{REQUEST_URI} ^index.php$
RewriteRule %1 - [F]

或者只是传递它RewriteRule

RewriteRule ^index.php$ do_some_stuff
于 2012-12-27T15:18:34.240 回答
2

您应该查看RewriteCond.htaccess 的(条件)检查http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/以获取大量信息和示例。

您也可以只为 index.php 编写一条规则,然后将其标记为最后一条规则[l]

RewriteRule ^/index.php(.*)   /index.php$1  [L]
RedirectRule ^/(.*)$   /index.php?path=$1  [L]
于 2012-12-27T15:18:39.747 回答