htaccess and php - redirects and pretty urls

2019-08-12 19:52发布

I have a webcommunity, and it's growing now. I like to do a link makeover for my web, and then I need to know the best solution for my case.

Right now my htaccess looks kind of like this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=user&username=$1 [L]

You are able to link to users like this domain.com/username and that's nice.

Then I have different pages like

  • index.php?page=forum&id=1
  • index.php?page=someotherpage&id=1&anotherid=5
  • index.php?page=3rd

... and so on. I want them to look something like this:

  • domain.com/forum/23/title-of-the-thread
  • domain.com/page2/id1/id2

... and so on.

How do I make these pretty urls without removing my domain.com/username functionality? What solution would you suggest?

I was thinking about creating a file that checks the URL, if it matches any pages, and users and so on. Then it will redirect with a header location.

4条回答
\"骚年 ilove
2楼-- · 2019-08-12 20:36

I was thinking about creating a file that checks the URL,

you actually have that file, it's index.php

if it matches any pages, and users and so on. Then it will redirect with a header location.

that's wrong. HTTP redirect won't make your URLs look "pretty"
you have to include appropriate file, not redirect to.

Just change your rule to more general one

RewriteRule ^(.*)$ index.php [L,QSA]
查看更多
唯我独甜
3楼-- · 2019-08-12 20:40

You basically have two options.

  1. Route all URLs to a central dispatcher (FrontController) and have that PHP script anaylze the URL and include the correct scripts
  2. Note every possible route (url rewrite) you have in the .htaccess

I've always worked with option 1, as this allows greatest flexibility with lowest mod_rewrite overhead. Option 2 may look something like:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^forum/([^/]+)/([^/]+)/?$ index.php?page=forum&id=$1 [L]
RewriteRule ^otherpage/([^/]+)/([^/]+)/?$ index.php?page=someotherpage&id=$1&anotherid=$21 [L]
RewriteRule ^page/([^/]+)/?$ index.php?page=$1 [L]
# …
RewriteRule ^([^/\.]+)/?$ index.php?page=user&username=$1 [L]

you said

I was thinking about creating a file that checks the URL, if it matches any pages, and users and so on. Then it will redirect with a header location.

While "creating a file that checks the URL" sounds a lot like option 1, "redirect with a header location" is the worst you could do. That would result in

  • an extra HTTP roundtrip for the client, leading to slower page loads
  • the "pretty URL" won't stick, the browser will show the URL you've redirected to
  • losing link-juice (SEO)
查看更多
在下西门庆
4楼-- · 2019-08-12 20:51

This can be done entirely with htaccess or php

//First Parameer
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?page=$1

//Second Parameter 
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ index.php?page=$1&username=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ index.php?page=$1&username=$2

read more about it here:
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/ http://www.roscripts.com/Pretty_URLs_-_a_guide_to_URL_rewriting-168.html

查看更多
We Are One
5楼-- · 2019-08-12 20:52

If all of the urls you are going to rewrite are going to the same end point, you could simply use:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

in index.php:

<?php
$url = $_SERVER['REQUEST_URI'];

How you use the request uri is up to you, you could for example use a simple strpos check:

<?php
$url = $_SERVER['REQUEST_URI'];

$rules = array(
    '/forum/' => 'forum',
    '/foo/' => 'foo',
    '/' => 'username'
);

foreach($rules as $pattern => $action) {
    if (strpos($url, $pattern) === 0) {
        // use action
        $file = "app/$action.php";
        require $file; 
        exit;
    }
}
// error handling - 404 no route found
查看更多
登录 后发表回答