如何在#ancors和htaccess中的查询之前从ulrs中删除* .php,index.php和尾部斜杠

编程入门 行业动态 更新时间:2024-10-28 18:32:54
本文介绍了如何在#ancors和htaccess中的查询之前从ulrs中删除* .php,index.php和尾部斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我无法找到我的问题的令人满意的答案.冲浪已经三天了,没有发现任何有效的方法.

我的网站结构如下:

· |__data |__controllers |__helpers |__partials |__layouts |__images |__javascripts |__stylesheets |__public |__index.php |__subfolder |__index.php |__other-content.php

由于只允许在服务器中使用www或html或html_public目录,因此我希望将内容与所有php和用户永远不会看到或使用的其他内容分开. /p>

我创建了一个名为/public的目录,并放置了用户将看到的所有内容.但是现在由于SEO的原因,我要美化网址.

我正在尝试删除php和html扩展名,删除index.php并删除所有链接(包括锚,查询和目录)中的结尾斜杠.

示例:

www.domain/必须为domain/

domain/必须为domain

domain/public/必须为domain

domain/public/index.php必须为domain

domain/public/subfolder/必须为domain/subfolder

domain/public/subfolder/index.php必须为domain/subfolder

domain/public/subfolder/#anchor必须为domain/subfolder#ancor

domain/public/subfolder/?query必须为domain/subfolder?query

domain/public/subfolder/other-content.php必须为domain/subfolder/other-content

domain/public/subfolder/other-content.php#ancor必须为domain/subfolder/other-content#ancor

domain/public/subfolder/other-content.php?query必须为domain/subfolder/other-content?query

我实际上还没有使用查询,因此以这种方式进行查询并不重要,因为我应该美化它们...

在htaccess中,我设法将www重定向到非www;将/public内容设置为根,并从url中隐藏它;删除尾部的斜杠(不在dirs中,也不在#ancors中);删除index.php

我试图强制从目录中删除结尾的斜杠,但我总是收到403消息,我也试图删除扩展名,但得到404消息.

这是我的htaccess:

# Options Options +FollowSymLinks +MultiViews -Indexes DirectorySlash off # Enable Rewrite Engine RewriteEngine on RewriteBase / # Exceptions RewriteCond %{REQUEST_URI} ^/(images|javascripts|stylesheets)/ [NC] RewriteRule ^ - [L] # www to non-www RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteCond %{HTTPS}s ^on(s)| RewriteRule ^ http%2://%1%{REQUEST_URI} [L,R=301] # Make /public like it was root RewriteCond %{REQUEST_URI} !^/public RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ /public/$1 [NC,L,QSA] # Remove trailing slash RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} (.+)/$ RewriteRule ^(.+)/$ /$1 [L,R=301] # Remove index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?$1 [L,QSA] RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC] RewriteRule ^ %1 [R=301,L] # Remove .php extention RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L] # Error pages ErrorDocument 403 /errors/403.php ErrorDocument 404 /errors/404.php ErrorDocument 500 /errors/500.php

编辑

我已经尝试过这个问题的解决方案,但它仍能在目录上显示403,而不会在末尾添加斜杠以及404 su文件(而没有php扩展名)!

另外,如果我键入domain/public,它将给我403.如果键入domain/public/,它将不会重定向到root.

# Options Options +FollowSymLinks +MultiViews -Indexes DirectorySlash off # Enable Rewrite Engine RewriteEngine on RewriteBase / # Exceptions RewriteCond %{REQUEST_URI} ^/(images|javascripts|stylesheets)/ [NC] RewriteRule ^ - [L] # www to non-www RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteCond %{HTTPS}s ^on(s)| RewriteRule ^ http%2://%1%{REQUEST_URI} [L,R=301] # Make /public like it was root RewriteCond %{REQUEST_URI} !^/public RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ /public/$1 [NC,L,QSA] # remove trailing slash RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s] RewriteRule ^(.+?)/$ /$1 [R=301,L] # To internally forward /dir/file to /dir/file.php RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC] RewriteRule ^(.+?)/?$ /$1.php [L]

解决方案

以这种方式关闭MultiViews:

Options +FollowSymLinks -MultiViews -Indexes DirectorySlash off RewriteEngine on RewriteBase / # Exceptions RewriteCond %{REQUEST_URI} ^/(images|javascripts|stylesheets)/ [NC] RewriteRule ^ - [L] # www to non-www RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteCond %1##%{HTTPS}s ^(.+)##(?:on(s)|) RewriteRule ^ http%2://%1%{REQUEST_URI} [L,R=301,NE] # Make /public like it was root RewriteCond %{THE_REQUEST} \s/+public/(\S*?)/?\s [NC] RewriteRule ^ /%1 [L,R=301,NE] # remove trailing slash from all URLs RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s] RewriteRule ^(.+)/$ /$1 [R=301,L,NE] # To externally redirect /dir/file.php to /dir/file RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC] RewriteRule ^ /%1 [R=301,NE,L] # Make /public like it was root RewriteCond %{REQUEST_URI} !^/public/ RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^.*$ public/$0 [L] # internally add trailing / to directories RewriteCond %{REQUEST_FILENAME} -d RewriteRule !/$ %{REQUEST_URI}/ [L] # To internally forward /dir/file to /dir/file.php RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC] RewriteRule ^(.+?)/?$ $1.php [L]

但是请记住,对于Web服务器,domain/与domain相同,并且在两种情况下Web服务器都会获取/.是您的浏览器在domain之后添加了/.

在测试此更改时清除浏览器缓存.

I am unable to find a satisfiable answer to my problem. It's been three days of surfing and finding nothing that actually works.

My site structure is the following:

· |__data |__controllers |__helpers |__partials |__layouts |__images |__javascripts |__stylesheets |__public |__index.php |__subfolder |__index.php |__other-content.php

Since I am allowed to only use the www or html or html_public directory in my server, I want to separate content from all the php and other stuff that users won't ever see nor use.

I created a directory called /public and put all content the user will see. But now I want to prettify urls for SEO reasons.

I am trying to remove the php and html extention, to remove index.php and to remove the trailing slash from all links including anchors, queries and directories.

Examples:

www.domain/ must be domain/

domain/ must be domain

domain/public/ must be domain

domain/public/index.php must be domain

domain/public/subfolder/ must be domain/subfolder

domain/public/subfolder/index.php must be domain/subfolder

domain/public/subfolder/#anchor must be domain/subfolder#ancor

domain/public/subfolder/?query must be domain/subfolder?query

domain/public/subfolder/other-content.php must be domain/subfolder/other-content

domain/public/subfolder/other-content.php#ancor must be domain/subfolder/other-content#ancor

domain/public/subfolder/other-content.php?query must be domain/subfolder/other-content?query

I actually don't use queries yet so it is not important to make them be in those ways, since I should prettify them...

In my htaccess I managed to redirect www to non-www; make /public content as root and hide it from url; remove trailing slash (not in dirs, nor in #ancors); remove index.php

I am trying to force removing the trailing slash from directories but I always get a 403 message, I am also trying to remove the extention but I get a 404 message.

Here is my htaccess:

# Options Options +FollowSymLinks +MultiViews -Indexes DirectorySlash off # Enable Rewrite Engine RewriteEngine on RewriteBase / # Exceptions RewriteCond %{REQUEST_URI} ^/(images|javascripts|stylesheets)/ [NC] RewriteRule ^ - [L] # www to non-www RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteCond %{HTTPS}s ^on(s)| RewriteRule ^ http%2://%1%{REQUEST_URI} [L,R=301] # Make /public like it was root RewriteCond %{REQUEST_URI} !^/public RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ /public/$1 [NC,L,QSA] # Remove trailing slash RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} (.+)/$ RewriteRule ^(.+)/$ /$1 [L,R=301] # Remove index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?$1 [L,QSA] RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC] RewriteRule ^ %1 [R=301,L] # Remove .php extention RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L] # Error pages ErrorDocument 403 /errors/403.php ErrorDocument 404 /errors/404.php ErrorDocument 500 /errors/500.php

EDIT

I have tried the solution from this question but it still give me 403 on directories without trailing slash plus 404 su files without the php extension!

Plus if I type domain/public it will give me 403. If I type domain/public/ it will not redirect to root.

# Options Options +FollowSymLinks +MultiViews -Indexes DirectorySlash off # Enable Rewrite Engine RewriteEngine on RewriteBase / # Exceptions RewriteCond %{REQUEST_URI} ^/(images|javascripts|stylesheets)/ [NC] RewriteRule ^ - [L] # www to non-www RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteCond %{HTTPS}s ^on(s)| RewriteRule ^ http%2://%1%{REQUEST_URI} [L,R=301] # Make /public like it was root RewriteCond %{REQUEST_URI} !^/public RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ /public/$1 [NC,L,QSA] # remove trailing slash RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s] RewriteRule ^(.+?)/$ /$1 [R=301,L] # To internally forward /dir/file to /dir/file.php RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC] RewriteRule ^(.+?)/?$ /$1.php [L]

解决方案

Have it this way with MultiViews turned off:

Options +FollowSymLinks -MultiViews -Indexes DirectorySlash off RewriteEngine on RewriteBase / # Exceptions RewriteCond %{REQUEST_URI} ^/(images|javascripts|stylesheets)/ [NC] RewriteRule ^ - [L] # www to non-www RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteCond %1##%{HTTPS}s ^(.+)##(?:on(s)|) RewriteRule ^ http%2://%1%{REQUEST_URI} [L,R=301,NE] # Make /public like it was root RewriteCond %{THE_REQUEST} \s/+public/(\S*?)/?\s [NC] RewriteRule ^ /%1 [L,R=301,NE] # remove trailing slash from all URLs RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s] RewriteRule ^(.+)/$ /$1 [R=301,L,NE] # To externally redirect /dir/file.php to /dir/file RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC] RewriteRule ^ /%1 [R=301,NE,L] # Make /public like it was root RewriteCond %{REQUEST_URI} !^/public/ RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^.*$ public/$0 [L] # internally add trailing / to directories RewriteCond %{REQUEST_FILENAME} -d RewriteRule !/$ %{REQUEST_URI}/ [L] # To internally forward /dir/file to /dir/file.php RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC] RewriteRule ^(.+?)/?$ $1.php [L]

But keep in mind that for a web server domain/ is same as domain and web server gets / in both cases. It is your browser that adds a / after domain.

Clear your browser cache when testing this change.

更多推荐

如何在#ancors和htaccess中的查询之前从ulrs中删除* .php,index.php和尾部斜杠

本文发布于:2023-11-01 19:35:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550220.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜杠   尾部   如何在   ancors   ulrs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!