Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Mod_rewrite-创建干净的URL_Php_Apache_Mod Rewrite - Fatal编程技术网

Php Mod_rewrite-创建干净的URL

Php Mod_rewrite-创建干净的URL,php,apache,mod-rewrite,Php,Apache,Mod Rewrite,我们想创建干净的网址到我们的网站。我搜索了如何做到这一点,并找到了一个代码片段,但它不符合我的要求。在这里,我将解释我们基本上想要什么 这是基本网站的URL: 我们有一个“页面”菜单,它列出了用户可以看到的特定页面。它看起来如下所示: 我们基本上希望,当有人访问这个/pages.php时,它应该将他们重定向到/pages(或者使用完整的URL:test.com/pages),或者不重定向,只显示未找到的页面 它还应适用于以下任何可能的配置: /页面 /页面/ /page.php 我看过Sta

我们想创建干净的网址到我们的网站。我搜索了如何做到这一点,并找到了一个代码片段,但它不符合我的要求。在这里,我将解释我们基本上想要什么

这是基本网站的URL:

我们有一个“页面”菜单,它列出了用户可以看到的特定页面。它看起来如下所示:

我们基本上希望,当有人访问这个/pages.php时,它应该将他们重定向到/pages(或者使用完整的URL:test.com/pages),或者不重定向,只显示未找到的页面

它还应适用于以下任何可能的配置:

  • /页面
  • /页面/
  • /page.php
我看过Stackowerflow的系统,它的工作方式符合我们的实际需要。所以每次我看到其中一个:

  • stackoverflow.com/questions
  • stackoverflow.com/questions/
  • stackoverflow.com/questions.php-显示未找到的页面站点
这基本上就是我需要的。我尝试过此代码,但它不能完全满足我的需要:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]
如果有人能写一个明确的解释,说明如何实现这一目标,我将很高兴。另外,如果你发现这篇文章重复,请链接我的确切职位,因为我一直在寻找这个确切的问题,但我没有找到任何答案


谢谢

在另一篇文章中找到了这个。它显然从url中删除了extensions.php、.html、.cgi

RewriteCond %{REQUEST_URI} (.*)/$
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule (.*)/$ $1.html [L]

RewriteCond %{REQUEST_URI} (.*)/$
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule (.*)/$ $1.php [L]

RewriteCond %{REQUEST_URI} (.*)/$
RewriteCond %{REQUEST_FILENAME}\.cgi -f
RewriteRule (.*)/$ $1.cgi [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f [OR]
RewriteCond %{REQUEST_FILENAME}\.php -f [OR]
RewriteCond %{REQUEST_FILENAME}\.cgi -f
RewriteRule .* %{REQUEST_FILENAME}/ [R=301,L]

下面,第一条规则将把*.php重定向到不带.php的URL,第二条规则将通过向其中添加“.php”来处理所有其他URL:

比如说,


/page.php
将重定向到
/page
,并将处理
page.php

好的,这样写

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName deli.localhost

    DocumentRoot /var/www/deli/
    <Directory /var/www/deli/>
        Options -Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all

                RewriteEngine on

                ## Externally redirect clients directly requesting .(php|aspx|html|htm|pl|cgi|asp)+ page 
                ## URIs to extensionless URIs
                ## If client request header contains php,aspx,html or htm file extension, redirect
                ## It avoids an infinite loop since "THE_REQUEST" is what the UA asks for, not what the 
                ## server translates it to on the second pass.
                ## THE_REQUEST syntax is something like: GET /page.html HTTP/1.0
                RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.(php|aspx|html|htm|pl|cgi|asp)+(/.*)?\ HTTP [NC]
                RewriteRule ^([^.]+)\.(php|aspx|html|htm|pl|cgi|asp)+(/.*)?$ http://xyz.com/$1$3 [NC,R=301,L]


                FileETag none





                ErrorDocument 401 /401.php
                ErrorDocument 403 /403.php
                ErrorDocument 404 /404.php
                ErrorDocument 500 /500.php
                ErrorDocument 503 /503.php
    </Directory>

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined
</VirtualHost>

服务器管理员webmaster@localhost
ServerName deli.localhost
DocumentRoot/var/www/deli/
选项-索引跟随符号链接多视图
不允许超限
命令允许,拒绝
通融
重新启动发动机
##外部重定向直接请求的客户端。(php | aspx | html | htm | pl | cgi | asp)+页面
##从URI到无扩展URI
##如果客户端请求头包含php、aspx、html或htm文件扩展名,请重定向
##它避免了无限循环,因为“_请求”是UA请求的,而不是用户请求的
##服务器在第二次传递时将其转换为。
##_请求语法类似于:GET/page.html HTTP/1.0
重写cond%{THE|u REQUEST}^[A-Z]{3,9}\/([^.]+)\(php | aspx | html | htm | pl | cgi | asp)+(/.*)\HTTP[NC]
重写规则^([^.]+)\(php | aspx | html | htm | pl | cgi | asp)+(/.*)$http://xyz.com/$1$3[NC,R=301,L]
FileTag无
ErrorDocument 401/401.php
ErrorDocument 403/403.php
ErrorDocument 404/404.php
ErrorDocument 500/500.php
ErrorDocument 503/503.php
ErrorLog/var/log/apache2/error.log
#可能的值包括:调试、信息、通知、警告、错误、临界值、,
#警惕,埃默格。
日志级别警告
CustomLog/var/log/apache2/access.log组合

查看我的答案,我已经选择了这些答案,他们关注的是如何从这样的URL生成一个干净的URL:但是你有没有任何解决方案可以简单地将pages.php转换为/pages?这是从文件中清理URL扩展名,而不是真正使其行为像来自数据库的SEO URL(也称为slug)(通常的模式)。它实际上与页面没有任何关系,即使我访问:/page.php或/page或/page/我应该只使用这些吗?没有别的吗?我会删除其他规则,至少看看它是否工作。但我的规则会导致静态文件(*.jpg)等问题,因此您可能需要一些附加条件。
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName deli.localhost

    DocumentRoot /var/www/deli/
    <Directory /var/www/deli/>
        Options -Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all

                RewriteEngine on

                ## Externally redirect clients directly requesting .(php|aspx|html|htm|pl|cgi|asp)+ page 
                ## URIs to extensionless URIs
                ## If client request header contains php,aspx,html or htm file extension, redirect
                ## It avoids an infinite loop since "THE_REQUEST" is what the UA asks for, not what the 
                ## server translates it to on the second pass.
                ## THE_REQUEST syntax is something like: GET /page.html HTTP/1.0
                RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.(php|aspx|html|htm|pl|cgi|asp)+(/.*)?\ HTTP [NC]
                RewriteRule ^([^.]+)\.(php|aspx|html|htm|pl|cgi|asp)+(/.*)?$ http://xyz.com/$1$3 [NC,R=301,L]


                FileETag none





                ErrorDocument 401 /401.php
                ErrorDocument 403 /403.php
                ErrorDocument 404 /404.php
                ErrorDocument 500 /500.php
                ErrorDocument 503 /503.php
    </Directory>

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined
</VirtualHost>