Warm tip: This article is reproduced from stackoverflow.com, please click
.htaccess php

Removing extensions with .htaccess but keeping Parameters causes unstyled page

发布于 2020-04-11 22:30:01

I'm trying to build a construct for redirecting or "beautifying" URL.

I would like to achieve following result:

  • URL like domain.com/signup.php should be accessed by domain.com/signup
  • Paramers should work like domain.com/signup/HASH will lead to domain.com/signup.php?hash=HASH
  • If possible multiple parameters should be extendable like domain.com/signup/HASH/VERIFIED -> domain.com/signup.php?hash=HASH&status=verified

My current approach is

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA]

I tried a lot, but either it doesn't work or it will end in an unstyled page. Any suggestions?

Questioner
18zehn
Viewed
84
Croises 2020-02-02 22:03

Use absolute links in your pages.
If you use any graphics or links in your pages, don't use "relative links". Instead, make all your links "absolute links" that start with http://www.example.com/....

Or a root-relative link, beginning the href value with /.

In your case: http://www.example.com/css/Stylesheet.css or /css/Stylesheet.css;

You can also add in the html <head>: <base href="/">

If you only use parameters at the root of the site, you can use:

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]

# with one parameter
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?hash=$2 [L,NE,QSA]

# with two parameters
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ $1.php?hash=$2&status=$3 [L,NE,QSA]