Change appearance of URLs with htaccess mod_rewrit

2019-09-02 20:35发布

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.

0条回答
登录 后发表回答