使用 htaccess 从 URL 中删除子目录

Remove subdirectory from URL using htaccess

我有以下 URL: http://example.org/search/search

使用 htaccess,我需要从 URL 中删除第一个 /search,以便 URL 是: http://example.org/search

我这辈子都不知道怎么办。 这是我的 htaccess 文件的相关部分

# Remove www from URL
RewriteCond %{HTTP_HOST} ^www\.example\.org$ [NC]
RewriteRule ^(.*)$ http://example.org/ [R=301,L]

# Remove the need for the php file extension
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ .php [NC,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.org/ [R=302,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ .php [L]

# Remove index from home URL
RewriteRule ^(.*)index\.php$ http://%{HTTP_HOST}/ [R=301,L]

谢谢

不确定您曾经使用过的规则。您有一些重复的规则和重定向会干扰路由。将所有重定向放在同一个地方,并确保在添加 php 扩展名之前使用 -f 检查:

# Remove www from URL
RewriteCond %{HTTP_HOST} ^www\.example\.org$ [NC]
RewriteRule ^(.*)$ http://example.org/ [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} \ /+(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.org/ [R=302,L]

# Remove index from home URL
RewriteCond %{THE_REQUEST} \ /+(.*)index\.php
RewriteRule ^(.*)index\.php$ http://%{HTTP_HOST}/ [R=301,L]

# Remove the search folder:
RewriteCond %{THE_REQUEST} \ /+search/search
RewriteRule ^search/search(.*)$ /search [L,R=301]

# Add the search folder back
RewriteCond  !^/search
RewriteRule ^search(.*)$ /search/search [L]

# Add the extension back on if it exists
RewriteCond %{DOCUMENT_ROOT}/\.php -f
RewriteRule ^(.*)$ .php [NC,L]