【问题标题】:Remove folder name from URL in codeigniter从 codeigniter 中的 URL 中删除文件夹名称
【发布时间】:2023-04-10 09:15:01
【问题描述】:

这是我的网址,例如:

example.com/abc/xyz/controller/function

在上面的 URL 中 abcxyz 是文件夹名称。

如何使用.htaccess从网址中删除“abc”和“xyz”?

这是我的.htaccess

RewriteEngine On
RewriteCond $1 !^(index\\.php|resources|robots\\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
RewriteCond %{REQUEST_URI} ^/?[^/]+/[^/]+/([^/]+)/([^/]+)/?$ [NC]
RewriteRule ^(.*)$ /%1/%2/? [NC,R,L]

【问题讨论】:

  • 只是确认 ... 在您的项目中您修改的 .htaccess 在哪里

标签: php .htaccess codeigniter-3


【解决方案1】:

要使用.htaccess 删除 example.com/abc/xyz/controller/function 中的 abcxyz,请执行以下操作:

Options +FollowSymlinks
RewriteEngine on
AddDefaultCharset UTF-8

RewriteCond %{REQUEST_URI} ^/?abc/xyz/controller/function/?$ [NC]
RewriteRule ^(.*)$ /controller/function [NC,R,L]

底部两行将从 example.com/letters/letters/letters/letters 格式的链接中删除前两个文件夹:

RewriteCond %{REQUEST_URI} ^/?[a-z]+/[a-z]+/([a-z]+)/([a-z]+)/?$ [NC]
RewriteRule ^(.*)$ /%1/%2/? [NC,R,L]

这接受文件夹名称的任何字符并从 URL 中删除前两个文件夹:

RewriteCond %{REQUEST_URI} ^/?[^/]+/[^/]+/([^/]+)/([^/]+)/?$ [NC]
RewriteRule ^(.*)$ /%1/%2/? [NC,R,L]

【讨论】:

  • abc 和 xyz 只是示例文件夹还是真的存在?
  • 例如,在我的情况下,我使用不同的名称。
  • 但是确实存在不同的名称
  • 好的,然后用我编辑的新的底部两行替换底部两行。
  • 您的文件夹中是否仅包含字母,或者是否包含其他字符,例如 - _ 0 1 2 3 等?
【解决方案2】:

你可以试试这个:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /folderName/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.


    ErrorDocument 404 /index.php
</IfModule>

【讨论】:

  • 这也将删除 index.php 文件或任何其他 /abc/xyz/ 。这只是一个例子。
猜你喜欢
  • 2019-07-18
  • 1970-01-01
  • 2014-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-19
  • 2016-04-13
  • 1970-01-01
相关资源
最近更新 更多