Rya*_*yan 4 apache .htaccess mod-rewrite apache-config
我有一个索引文件,可以根据n个 PATH_INFO变量构建内容.
例:
site.com/A/B/n/
Run Code Online (Sandbox Code Playgroud)
应该使用index.php:
site.com/index.php?var1=A&var2=B&varN=n
- or -
site.com/index.php/A/B/n/
Run Code Online (Sandbox Code Playgroud)
代替:
site.com/A/B/n/index.php || which doesn't exist ||
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经尝试了许多变体:
RedirectMatch ^/.+/.*$ /
Run Code Online (Sandbox Code Playgroud)
没有成功.
我在这里有一个不优雅和不可扩展的解决方案:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?p1=$1 [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?p1=$1&p2=$2 [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?p1=$1&p2=$2&p3=$3 [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?p1=$1&p2=$2&p3=$3&p4=$4 [NC,L]
Run Code Online (Sandbox Code Playgroud)
此解决方案的问题:
你能帮我吗?
选项1: (site.com/index.php?var1=A&var2=B&varN=n):
Options +FollowSymLinks -MultiViews
RewriteEngine On
# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
RewriteRule ^([^/]+)/?$ index.php?p1=$1 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?p1=$1&p2=$2 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?p1=$1&p2=$2&p3=$3 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?p1=$1&p2=$2&p3=$3&p4=$4 [QSA,L]
Run Code Online (Sandbox Code Playgroud)
你有[NC]旗帜......所以没有必要A-Z在你的模式中.
2.而不是[a-zA-Z0-9_-\+\=\&]或[a-zA-Z0-9_-]我使用[^/]哪个意味着除了斜杠/之外的任何字符.
3. [QSA]添加了标志以保留现有的查询字符串.
选项2: (site.com/index.php/A/B/n/):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php/$1 [L]
Run Code Online (Sandbox Code Playgroud)
实际上,如果您不打算在任何地方显示该URL(例如,301重定向等),最后一行可以轻松替换为RewriteRule .* index.php [L]- 您将$_SERVER['REQUEST_URI']在PHP代码中查找原始URL .
| 归档时间: |
|
| 查看次数: |
11939 次 |
| 最近记录: |