K
K
kayden2018-12-22 21:03:03
Apache HTTP Server
kayden, 2018-12-22 21:03:03

How to redirect http and www and hide folders in url and file extension?

Good afternoon, I've been struggling with the problem for two days now.
The bottom line is this
There is the following structure
/pages/yslugi.php
/pages/
/
I want to make redirects from www and https
to remove the file extension in the url at the end, while not opening the path with the extension
Example: https://site.ru/ yslugi BUT!!! https://site.ru/yslugi.php should be in the middle or not found
and removed from the url folder pages
like the problem is not difficult, I found solutions separately
RewriteCond %{DOCUMENT_ROOT}/pages/$1 -f
RewriteRule ^(.*)$ pages/$1 [L,QSA]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI}[R=301,L]
RewriteCond %{HTTP_HOST} ^www.site.ru$ [NC]
RewriteRule ^(.*)$ https://site.ru/$1 [R=301,L]
but all together it doesn't works, I didn’t find a solution for closing paths with an extension
(I found an option when both options work).
If anyone has a solution to this issue I would be grateful.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
dodo512, 2018-12-23
@dodo512

RewriteEngine On

RewriteCond %{THE_REQUEST}  " /(pages/|[^?]*\.php)" [OR]
RewriteCond %{SERVER_PORT}  !443    [OR]
RewriteCond %{HTTP_HOST}    ^www\.  [NC]
RewriteRule ^(pages/|)(.*?)(\.php|$) https://site.ru/$2 [R=301,L]

RewriteCond %{DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule ^(.*)$ pages/$1.php [L,QSA]

V
Viktor Taran, 2018-12-24
@shambler81

Here is the answer about the redirect
https://klondike-studio.ru/standards/standartnyy-h...
As for shortening folders in urls while continuing to work in them
There are two options
1 create a soft link
2. if you have nginx + apache then you can easily implement it on nginx
3. don’t invent a wheel, such tasks simply don’t happen, most likely you are trying to solve a certain task with an inappropriate method, describe in more detail why you are doing this and what do you want to achieve with this?

K
kayden, 2018-12-26
@kayden

The .htaccess file is only at the root.
The task seems to be simple, and the language does not turn to be called a bicycle.
the solution was simple and convenient.
there was a .htaccess code and it worked fine, before the https and www task
# If the requested object is not a folder
RewriteCond %{REQUEST_FILENAME} !-d
# If the requested object with a php extension is a file
RewriteCond %{REQUEST_FILENAME}\.php -f
# Do a replacement with appending ".php"
RewriteRule ^(.*)$ $1.php
RewriteCond %{DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule (.*)$ pages/$1.php [L,QSA]
in the pages folder are some kind of controllers that are connected from the views
/ css
/ img
/ views
/ pages display folder
/
at the root of the .htaccess
path were site.ru/yslugi
why specify the pages folder in the path,
you can of course solve the issue simply by throwing the "controllers" out of the pages folder into the root, but then it will turn out to be a mess
to achieve the lack of pages in the url, because the site has already worked like this, and it's just not beautiful
and only https breaks the logic

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question