简体   繁体   中英

.htaccess rewrite multiple urls

I need help with this rewrite in .htaccess file. So this what I have now ans this works but when I try to add a new RewriteRule nothing happens. I the url that I want to be rewrite is index.php?page=$1

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1

So when I do it like:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*)$ index.php?page=$1

The page doesn't have any css when i do it like:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*_)$ index.php?page=$1

The page has css but i still get index.php?page=pagetitle. But the profile page does give me / username .

RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*)$ index.php?page=$1

Your are asking the server to redirect every URL to two different pages, it cannot work the server cannot just guess what page to load.

What you need is either a /profile/username rule or a /page/pagetitle rule. IE something like:

RewriteRule ^profile/(.*)$ profile.php?username=$1 [QSA]
RewriteRule ^(.*)$ index.php?page=$1 [L]

Your rewrite rules are based on regular expressions and therefore need to be as specific as possible so the server can determine exactly which url to use - for example how can you tell if http://example.com/something is a page or a profile? Using a prefix such as "user", "profile", etc on your URLs means that http://example.com/profile/something can be redirected as as a username with a default redirect for everything else. To accomplish this you need to make the more specific pattern match first (users) and utilized the [L] directive to indicate that following rules should not be processed. I usually use a negative character class for URLs to match anything except a forward slash - [^/]* .

# Enable mod_rewrite
RewriteEngine On
# Set the base directory
RewriteBase /
# Don't process if this is an actual file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Does this url start with /profile and then followed with additional characters?
RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,L]
# Assume everything else is a page
RewriteRule ^(.*)$ index.php?page=$1 [NC,L]

Test at http://htaccess.madewithlove.be/ (note that %{REQUEST_FILENAME} and %{REQUEST_FILENAME} aren't supported for testing).

Profile

input url
http://www.example.com/profile/something

output url
http://www.example.com/profile.php

debugging info
1 RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,QSA,L]  
    This rule was met, the new url is http://www.example.com/profile.php
    The tests are stopped because the L in your RewriteRule options
2 RewriteRule ^(.*)$ index.php?page=$1 [NC,L]

Page

input url
http://www.example.com/something

output url
http://www.example.com/index.php

debugging info
1 RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,L]  
2 RewriteRule ^(.*)$ index.php?page=$1 [NC,L]
    This rule was met, the new url is http://www.example.com/index.php
    The tests are stopped because the L in your RewriteRule options

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM