Change appearance of URLs with htaccess mod_rewrite RewriteRule

一个人想着一个人 提交于 2020-01-06 05:35:28

问题


It's really tough to find clear explanations of the Rewrite Engine syntax anywhere. Regular expressions are familiar, but the rest of it is alien to me.

Is there no way to hide http:// or https:// in the URL that is displayed in the address bar?

My directory looks like this.

public_html/
    example/
        index.php
        whatever.php

Issue #1

This is currently what's happening, which obviously all over the place.

URL THAT IS TYPED IN                         URL THAT DISPLAYS

https://www.example.com -------------------> https://www.example.com
https://example.com -----------------------> https://example.com
http://www.example.com --------------------> https://www.example.com/example/index.php
http://example.com ------------------------> https://example.com/example/index.php
www.example.com ---------------------------> https://www.example.com/example/index.php
example.com -------------------------------> https://example.com/example/index.php

The first thing I'd like to do is always force the following.

URL THAT IS TYPED IN                          URL THAT DISPLAYS

https://www.example.com/example/index.php --> example.com/index
https://www.example.com --------------------> example.com
https://example.com ------------------------> example.com
http://www.example.com ---------------------> example.com
http://example.com -------------------------> example.com
www.example.com ----------------------------> example.com
example.com --------------------------------> example.com

Issue #2

The second thing I'd like to do, which I just figured out how to do, is clean up the URLs for sub-directories and files.

ACCESS TO THIS FILE PATH                      URL THAT DISPLAYS

example.com/example/index.php --------------> example.com/index

I know this can be done with the following RewriteRule.

RewriteRule ^index /mydomain/index.php [NC]

But I'd also like to force the URLs to display in the following way no matter what.

URL THAT IS TYPED IN                          URL THAT DISPLAYS

https://www.example.com/example/index.php --> example.com/index
https://example.com/example/index.php ------> example.com/index
http://www.example.com/example/index.php ---> example.com/index
http:/example.com/example/index.php --------> example.com/index
www.example.com/example/index.php ----------> example.com/index
mydomain.com/example/index.php -------------> example.com/index

https://www.example.com/index.php ----------> example.com/index
https://example.com/index.php --------------> example.com/index
http://www.example.com/index.php -----------> example.com/index
http:/example.com/index.php ----------------> example.com/index
www.example.com/index.php ------------------> example.com/index
mydomain.com/index.php ---------------------> example.com/index

https://www.example.com/index --------------> example.com/index
https://example.com/index ------------------> example.com/index
http://www.example.com/index ---------------> example.com/index
http://example.com/index -------------------> example.com/index
www.example.com/index ----------------------> example.com/index
example.com/index --------------------------> example.com/index

I did just find this, so I'm going to start reading right after dinner.

I also came across this right now, but I'm still struggling to wrap my head around this.

My Progress So Far

RewriteCond %{REQUEST_URI} ^/login$
RewriteRule ^login /index.php [NC]

RewriteCond %{REQUEST_URI} ^/dashboard$
RewriteRule ^dashboard /dashboard.php [NC]

RewriteCond %{REQUEST_URI} ^/logout$
RewriteRule ^logout /includes/destroy.php [NC]

RewriteCond %{HTTP_HOST} ^www\.birddogdata\.com$
RewriteRule ^(.*)$ https://birddogdata.com [R=301]

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

The only issue I have yet to figure out is how to get example.com to redirect to example.com/login.

Any help would be appreciated.

来源:https://stackoverflow.com/questions/52960868/change-appearance-of-urls-with-htaccess-mod-rewrite-rewriterule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!