1

I have a directory structure as follows:

/gallery
----index.php
----/23XASDTAGH
----/24XGA43KJA/

I'd like to use mod rewrite to rewrite if a directory exists. so:

www.example.com/gallery/23XASDTAGH/

becomes

www.example.com/gallery/index.php?gallery=23XASDTAGH

but i'd like to do this silently, so no changes happen to the url. now i have this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}/ -d
RewriteRule (.*) /gallery/index.php?gallery=$1 [L]

which works with

www.example.com/gallery/23XASDTAGH/

but the odd thing is when i leave the trailing slash off the end it changes the url to

www.example.com/gallery/23XASDTAGH/?gallery=23XASDTAGH

how can i get it to work with or without the trailing slash?

4

3 回答 3

1

I think the following will do the trick

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) /gallery/index.php?gallery=$1 [L]

Second line checks that the requested filename is a directory and if it is then the third line does the actual rewrite

To try and deal with the trailing slash problem, maybe this, or something like this will work

RewriteCond %{REQUEST_FILENAME} (.*)/?$
RewriteCond %1 -d
RewriteRule (.*)/$ /gallery/index.php?gallery=$1 [R=301]
于 2012-12-02T00:39:35.670 回答
0

你可能想试试这个:

RewriteEngine on
RewriteRule ^(gallery)/([a-zA-Z0-9-_=]+)/?$ http://www.example.com/$1/index.php?$1=$2 [R=301,L]

永久重定向输入的 URL(浏览器地址栏中的那个):

www.example.com/gallery/23XASDTAGHwww.example.com/gallery/23XASDTAGH/

www.example.com/gallery/index.php?gallery=23XASDTAGH

于 2012-12-02T01:41:34.717 回答
-1

终于弄明白了,我需要?/在重写规则中的表达式末尾加上一个来说明斜杠是可选的。为什么正则表达式必须如此难易。8-)

正确表达:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}     -d
RewriteRule ^([a-zA-Z0-9_-]+)?/$    /gallery/index.php?gallery=$1 [L]
于 2012-12-02T02:17:29.903 回答