htaccess 从 url 中删除 index.php

发布于 2024-10-05 21:26:46 字数 245 浏览 0 评论 0原文

我遇到一个问题,谷歌用错误的网址索引了一些页面。

他们索引的网址是:

http://www.example.com/index.php/section1/section2

我需要它重定向到:

http://www.example.com/section1/section2

.htaccess 不是我的强项,因此任何帮助将不胜感激。

I have a problem whereby google has indexed some pages with the wrong url.

The url they are indexing is:

http://www.example.com/index.php/section1/section2

I need it to redirect to:

http://www.example.com/section1/section2

.htaccess isn't my forte, so any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

冰魂雪魄 2024-10-12 21:26:46

原来的答案实际上是正确的,但缺乏解释。我想添加一些解释和修改。

我建议阅读这个简短的介绍 https://httpd.apache.org/docs/2.4 /rewrite/intro.html(15 分钟)并在阅读时参考这两页。

https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
https://httpd.apache.org/docs/2.4/rewrite/flags.html


这是从 URL 中隐藏 index.php 的基本规则。将其放入根 .htaccess 文件中。

mod_rewrite 必须在 PHP 中启用,这适用于高于 5.2.6 的 PHP 版本。

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

%{REQUEST_FILENAME} 视为主机之后的路径。

例如 https://www.example.com/index.html%{REQUEST_FILENAME}/index.html

所以最后 3 行意味着,如果它不是常规文件 !-f 也不是目录 !-d,则执行 RewriteRule

至于 RewriteRule 格式:

在此处输入图像描述

所以 RewriteRule (.*) /index.php/$1 [L] 的意思是,如果满足 2 个 RewriteCond,它 (.*) 将匹配主机名后面的所有内容。 . 匹配任何单个字符,.* 匹配任何字符(.*) 使其成为变量引用 $1,然后替换为 /index.php/$1。最终的效果就是在整个URL路径中添加一个前面的index.php

例如,对于 https://www.example.com/hello,它将生成 https://www.example.com/index.php/hello 内部。

另一个关键问题是,这确实解决了问题。 内部,(我猜)它总是需要https://www.example.com/index.php/hello,但是通过重写,您可以访问该网站而无需 < code>index.php,apache 在内部为您添加它。


顺便说一句,Apache 文档并不非常建议创建额外的 .htaccess 文件。

重写通常在主服务器配置中配置
设置(在任何 部分之外)或在 内部
容器。这是最简单的重写方法,推荐

The original answer is actually correct, but lacks explanation. I would like to add some explanations and modifications.

I suggest reading this short introduction https://httpd.apache.org/docs/2.4/rewrite/intro.html (15mins) and reference these 2 pages while reading.

https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
https://httpd.apache.org/docs/2.4/rewrite/flags.html


This is the basic rule to hide index.php from the URL. Put this in your root .htaccess file.

mod_rewrite must be enabled with PHP and this will work for the PHP version higher than 5.2.6.

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

Think %{REQUEST_FILENAME} as the the path after host.

E.g. https://www.example.com/index.html, %{REQUEST_FILENAME} is /index.html

So the last 3 lines means, if it's not a regular file !-f and not a directory !-d, then do the RewriteRule.

As for RewriteRule formats:

enter image description here

So RewriteRule (.*) /index.php/$1 [L] means, if the 2 RewriteCond are satisfied, it (.*) would match everything after the hostname. . matches any single character , .* matches any characters and (.*) makes this a variables can be references with $1, then replace with /index.php/$1. The final effect is to add a preceding index.php to the whole URL path.

E.g. for https://www.example.com/hello, it would produce, https://www.example.com/index.php/hello internally.

Another key problem is that this indeed solve the question. Internally, (I guess) it always need https://www.example.com/index.php/hello, but with rewriting, you could visit the site without index.php, apache adds that for you internally.


Btw, making an extra .htaccess file is not very recommended by the Apache doc.

Rewriting is typically configured in the main server configuration
setting (outside any <Directory> section) or inside <VirtualHost>
containers. This is the easiest way to do rewriting and is recommended

你好,陌生人 2024-10-12 21:26:46

从 URL 中删除 index.php,并将访问者重定向到页面的非 index.php 版本:

RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

这将干净地重定向 /index.php/myblog简单地/myblog

使用 301 重定向将保留 Google 搜索引擎排名。

To remove index.php from the URL, and to redirect the visitor to the non-index.php version of the page:

RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

This will cleanly redirect /index.php/myblog to simply /myblog.

Using a 301 redirect will preserve Google search engine rankings.

你好,陌生人 2024-10-12 21:26:46
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
小情绪 2024-10-12 21:26:46

假设现有的 url 是

http://example.com/index.php/foo/bar

,我们想将其转换为

 http://example.com/foo/bar

您可以使用以下规则:

RewriteEngine on
#1) redirect the client from "/index.php/foo/bar" to "/foo/bar"
RewriteCond %{THE_REQUEST} /index\.php/(.+)\sHTTP [NC]
RewriteRule ^ /%1 [NE,L,R]
#2)internally map "/foo/bar" to "/index.php/foo/bar"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php/$1 [L]

在 spep #1 中,我们首先匹配请求字符串并捕获 /index.php/ 和捕获的值保存在 %1 var 中。然后我们将浏览器发送到一个新的 url。
#2 在内部处理该请求。当浏览器到达 /foo/bar 时,#2rule 将新的 url 重写到原始位置。

Assuming the existent url is

http://example.com/index.php/foo/bar

and we want to convert it into

 http://example.com/foo/bar

You can use the following rule :

RewriteEngine on
#1) redirect the client from "/index.php/foo/bar" to "/foo/bar"
RewriteCond %{THE_REQUEST} /index\.php/(.+)\sHTTP [NC]
RewriteRule ^ /%1 [NE,L,R]
#2)internally map "/foo/bar" to "/index.php/foo/bar"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php/$1 [L]

In the spep #1 we first match against the request string and capture everything after the /index.php/ and the captured value is saved in %1 var. We then send the browser to a new url.
The #2 processes the request internally. When the browser arrives at /foo/bar , #2rule rewrites the new url to the orignal location.

居里长安 2024-10-12 21:26:46

从 WordPress 网站的 url 中删除 index.php 的步骤。

  1. 检查您的服务器是否启用了 mod_rewrite。
    要检查它是否已启用 - 使用以下命令在根文件夹中创建 1 个文件 phpinfo.php。
 <?php
   phpinfo?();
 ?>

现在运行这个文件 - www.yoursite.com/phpinfo.php ,它将在加载模块部分显示 mod_rewrite 。
如果未启用,则在终端上执行以下命令。

sudo a2enmod rewrite
sudo service apache2 restart
  1. 确保您的 .htaccess 存在于 WordPress 根文件夹中,如果不存在,请创建一个 .htaccess 文件
    将此代码粘贴到您的 .htaccess 文件中:-
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.php$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.php [L]
</IfModule>  
  1. 进一步将 .htaccess 的权限设置为 666,以便它变得可写,现在您可以在 WordPress 永久链接中进行更改。

  2. 现在转到设置 ->永久链接 ->并更改为您需要的 url 格式。
    删除此代码 /index.php/%year%/%monthnum%/%day%/%postname%/
    并在自定义结构中插入此代码: /%postname%/

  3. 如果仍然不成功,请检查您的托管,我的是 digitalocean 服务器,所以我自己清除了它

编辑了文件 /etc/apache2/sites-enabled/000-default.conf

在 DocumentRoot /var/www/html 之后添加了这一行

<Directory /var/www/html>
   AllowOverride All
</Directory>

重新启动您的 apache 服务器

注意:/var/www/html 将是你的文档根目录

Steps to remove index.php from url for your wordpress website.

  1. Check you should have mod_rewrite enabled at your server.
    To check whether it's enabled or not - Create 1 file phpinfo.php at your root folder with below command.
 <?php
   phpinfo?();
 ?>

Now run this file - www.yoursite.com/phpinfo.php and it will show mod_rewrite at Load modules section.
If not enabled then perform below commands at your terminal.

sudo a2enmod rewrite
sudo service apache2 restart
  1. Make sure your .htaccess is existing in your WordPress root folder, if not create one .htaccess file
    Paste this code at your .htaccess file :-
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.php$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.php [L]
</IfModule>  
  1. Further make permission of .htaccess to 666 so that it become writable and now you can do changes in your wordpress permalinks.

  2. Now go to Settings -> permalinks -> and change to your needed url format.
    Remove this code /index.php/%year%/%monthnum%/%day%/%postname%/
    and insert this code on Custom Structure: /%postname%/

  3. If still not succeeded then check your hosting, mine was digitalocean server, so I cleared it myself

Edited the file /etc/apache2/sites-enabled/000-default.conf

Added this line after DocumentRoot /var/www/html

<Directory /var/www/html>
   AllowOverride All
</Directory>

Restart your apache server

Note: /var/www/html will be your document root

拥醉 2024-10-12 21:26:46

执行以下步骤

1. 确保主机/您的电脑 mod_rewrite 模块处于活动状态。如果未激活,则尝试以某种方式激活,打开 httpd.conf 文件。您可以在 phpinfo.php 中检查这一点以找出答案。

更改此设置:

#LoadModule rewrite_module modules/mod_rewrite.so

并重新启动 wamp

LoadModule rewrite_module modules/mod_rewrite.so

2. 然后转到 .htaccess 文件,并尝试修改为:

RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]

如果上述不起作用,请尝试使用此:

RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

3. 将.htaccess文件移动到根目录,其中有index.php。

www OR root folder
- index.php
- .htaccess

Do the following steps

1. Make sure that the hosting / your pc mod_rewrite module is active. if not active then try to activate in a way, open the httpd.conf file. You can check this in the phpinfo.php to find out.

change this setting :

#LoadModule rewrite_module modules/mod_rewrite.so

to be and restart wamp

LoadModule rewrite_module modules/mod_rewrite.so

2. Then go to .htaccess file, and try to modify to be:

RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]

if above does not work try with this:

RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

3. Move .htaccess file to root directory, where is index.php there.

www OR root folder
- index.php
- .htaccess
山色无中 2024-10-12 21:26:46

有些人可能会使用 mod_rewrite 使用上面列出的方法得到 403。另一种重写index.php的解决方案如下:

<IfModule mod_rewrite.c> 

RewriteEngine On

# Put your installation directory here:
RewriteBase /

# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

</IfModule> 

Some may get a 403 with the method listed above using mod_rewrite. Another solution to rewite index.php out is as follows:

<IfModule mod_rewrite.c> 

RewriteEngine On

# Put your installation directory here:
RewriteBase /

# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

</IfModule> 
找回味觉 2024-10-12 21:26:46

我使用了上述部分中的许多代码来从基本网址中删除index.php。但从我的角度来看,它不起作用。因此,您可以使用我已经使用过的代码并且它可以正常工作。

如果您确实需要从基本 URL 中删除 index.php,那么只需将此代码放入您的 htaccess 中即可。

RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]

RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

I have used many codes from the above mentioned sections for removing index.php form the base url. But it was not working from my end. So, you can use this code which I have used and its working properly.

If you really need to remove index.php from the base URL then just put this code in your htaccess.

RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]

RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
韬韬不绝 2024-10-12 21:26:46

这将起作用,在 .htaccess 文件中使用以下代码

重写引擎开启

# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]

This will work, use the following code in .htaccess file

RewriteEngine On

# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]

星星的轨迹 2024-10-12 21:26:46

我不需要很多庞大的代码来给出一个小片段就可以解决我的问题。

我有https://example.com/entitlements/index.php 相反,我希望任何键入它的人在输入 https://example.com/entitlements/index
你仍然会得到错误,因为包含这个词“index”,尽管index.php的内容仍然会正确显示,但总会有一个错误被抛出

cletus 在“https://stackoverflow.com/a/1055655/12192635”上发帖,其中
解决了

使用以下内容编辑您的 .htaccess 文件
将访问 https://example.com/entitlements/index.php 的用户重定向到 404 页面

RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]

将访问 https://example.com/entitlements/index 的人重定向到 404 页面

RewriteCond %{THE_REQUEST} \index[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]

尽管我们有已经知道上面的代码可以与堆栈上已有的代码一起使用,请参阅我在其末尾的所有代码下方应用上面的代码的位置。

# The following will allow you to use URLs such as the following:
#
#   example.com/anything
#   example.com/anything/
#
# Which will actually serve files such as the following:
#
#   example.com/anything.html
#   example.com/anything.php
#
# But *only if they exist*, otherwise it will report the usual 404 error.

Options +FollowSymLinks
RewriteEngine On

# Remove trailing slashes.
# e.g. example.com/foo/ will redirect to example.com/foo
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]

# Redirect to HTML if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

# Redirect to PHP if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]

RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]

RewriteCond %{THE_REQUEST} \index[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]

I don't have to many bulky code to give out just a little snippet solved the issue for me.

i have https://example.com/entitlements/index.php rather i want anyone that types it to get error on request event if you type https://example.com/entitlements/index
you will still get error since there's this word "index" is contained there will always be an error thrown back though the content of index.php will still be displayed properly

cletus post on "https://stackoverflow.com/a/1055655/12192635" which
solved it

Edit your .htaccess file with the below
to redirect people visiting https://example.com/entitlements/index.php to 404 page

RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]

to redirect people visiting https://example.com/entitlements/index to 404 page

RewriteCond %{THE_REQUEST} \index[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]

Not withstanding we have already known that the above code works with already existing codes on stack see where i applied the code above just below the all codes at it end.

# The following will allow you to use URLs such as the following:
#
#   example.com/anything
#   example.com/anything/
#
# Which will actually serve files such as the following:
#
#   example.com/anything.html
#   example.com/anything.php
#
# But *only if they exist*, otherwise it will report the usual 404 error.

Options +FollowSymLinks
RewriteEngine On

# Remove trailing slashes.
# e.g. example.com/foo/ will redirect to example.com/foo
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]

# Redirect to HTML if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

# Redirect to PHP if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]

RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]

RewriteCond %{THE_REQUEST} \index[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]
执笏见 2024-10-12 21:26:46

试试这个,它对我有用

<IfModule mod_rewrite.c>

# Enable Rewrite Engine
# ------------------------------
RewriteEngine On
RewriteBase /

# Redirect index.php Requests
# ------------------------------
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]

# Standard ExpressionEngine Rewrite
# ------------------------------
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

</IfModule>

try this, it work for me

<IfModule mod_rewrite.c>

# Enable Rewrite Engine
# ------------------------------
RewriteEngine On
RewriteBase /

# Redirect index.php Requests
# ------------------------------
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]

# Standard ExpressionEngine Rewrite
# ------------------------------
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

</IfModule>

月寒剑心 2024-10-12 21:26:46

THE_REQUEST 与大多数答案一样,应被视为最后的手段。最好使用 REQUEST_URI

以下代码执行 301 重定向,并适用于 /index.php/index.php/foo/bar

RewriteCond %{REQUEST_URI} ^/index\.php/(.+)
RewriteRule ^ /%1 [NE,L,R=301]

THE_REQUEST as in most of the answers should be considered a last resort. Better use REQUEST_URI.

The following performs a 301 redirect and works with both /index.php and /index.php/foo/bar.

RewriteCond %{REQUEST_URI} ^/index\.php/(.+)
RewriteRule ^ /%1 [NE,L,R=301]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文