2010-12-10 101 views
0

這是我的應用程序使用如何讓我的應用程序URL漂亮(SEF)與htaccess的

http://mydomain.com/index.php 
http://mydomain.com/video.php?id=29 
http://mydomain.com/news.php?id=33 
http://mydomain.com/photos.php?albumid=10 
http://mydomain.com/archive.php 
http://mydomain.com/album.php 
http://mydomain.com/contact.php 

我想讓米的URL看起來乾淨htaccess的像

http://mydomain.com/video/id/29 
http://mydomain.com/news/id/33 
http://mydomain.com/photos/albumid/10 
http://mydomain.com/archive 
http://mydomain.com/album 
http://mydomain.com/contact 

問題的網址較舊的應用程序已經在根目錄中使用了大量的htaccess代碼。這裏是在htaccess中已經存在的代碼。

php_value post_max_size 100M 
php_value upload_max_filesize 100M 
php_value memory_limit 100M 

# 
# Apache/PHP/osTube settings: 
# 

# Don't show directory listings for URLs which map to a directory. 
#Options -Indexes 

# Follow symbolic links in this directory. 
#Options +FollowSymLinks 

# Set the default handler. 
#DirectoryIndex index.php 

# Override some PHP settings. 

# PHP 4, Apache 1. 
<IfModule mod_php4.c> 
    php_value error_reporting 6135 
</IfModule> 

# PHP 4, Apache 2. 
<IfModule sapi_apache2.c> 
    php_value error_reporting 6135 
</IfModule> 

# PHP 5, Apache 1 and 2. 
<IfModule mod_php5.c> 
    php_value error_reporting 6135 
</IfModule> 

# Deny access to certain files 
<Files "convert.php"> 
Order Deny,Allow 
Deny from All 
</Files> 

# Various rewrite rules. 
<IfModule mod_rewrite.c> 
    RewriteEngine on 

    # If your site can be accessed both with and without the prefix www. you 
    # can use one of the following settings to force user to use only one option: 
    # 
    # If you want the site to be accessed WITH the www. only, adapt and 
    # uncomment the following: 
    # RewriteCond %{HTTP_HOST} ^example\.com$ [NC] 
    # RewriteRule .* http://www.example.com/ [L,R=301] 
    # 
    # If you want the site to be accessed only WITHOUT the www. prefix, adapt 
    # and uncomment the following: 
    # RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC] 
    # RewriteRule .* http://example.com/ [L,R=301] 

    # Modify the RewriteBase if you are using osTube in a subdirectory and 
    # the rewrite rules are not working properly. 
    # RewriteBase /ostube 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^Home(.*)$ index.php$1 [L,QSA,NC] 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(video|audio|document|photo)/([0-9]+)(/[^/]+)?/?(.*)$ mediadetails.php?mid=$2&$4 [L,QSA,NC] 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^Video(.*)$ media.php?type=video$1 [L,QSA,NC] 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^Audio(.*)$ media.php?type=audio$1 [L,QSA,NC] 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^Document(.*)$ media.php?type=document$1 [L,QSA,NC] 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^Photo(.*)$ media.php?type=photo$1 [L,QSA,NC] 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^Tags(.*)$ tags.php$1 [L,QSA,NC] 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^Community(.*)$ community.php$1 [L,QSA,NC] 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^weblog/([0-9]+)(/[^/]+)?/?(.*)$ blog.php?action=post&pid=$1&$3 [L,QSA,NC] 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^Weblog(.*)$ blog.php$1 [L,QSA,NC] 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^Channel/(video|audio|document|photo)/([0-9]+)(/[^/]+)?/?(.*)$ media.php?type=$1&chid=$2$4 [L,QSA,NC] 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^Tag/([0-9]+)/?(.*)$ search_result.php?tid=$1&$2 [L,QSA,NC] 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^User/([^/]+)/?(.*)$ userprofile.php?uname=$1$2 [L,QSA,NC] 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^WebTV/([^/]+)/([0-9]+)(.*)$ webtv.php?mode=$1&id=$2$3 [L,QSA,NC] 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^WebTV(.*)$ webtv.php$1 [L,QSA,NC] 

    RewriteRule ^flvplayer.swf vimp.swf [L,QSA,NC] 
    RewriteCond %{QUERY_STRING} ^mediaid=(\d+)&action=getmediainfo$ 
    RewriteRule ^$ flashcomm.php?mediaid=%1&action=getmediainfo [L,NC] 
    RewriteCond %{QUERY_STRING} ^action=getmediainfo&mediaid=(\d+)$ 
    RewriteRule ^$ flashcomm.php?mediaid=%1&action=getmediainfo [L,NC] 

    #RewriteCond %{REQUEST_FILENAME} !-f 
    #RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^sitemap.xml$ sitemap.php [L,QSA,NC] 
</IfModule> 

,我應該使用什麼樣的代碼Rewrite_rule也是,如果有人能指導我的教程我會在哪裏能夠理解htaccess以及也正則表達式我將不勝感激。

此外,我想禁用文件查看選項索引或我不記得確切的東西。

謝謝你..

回答

0

請嘗試以下

#Turn off option indexes 
Options -Indexes 

RewriteCond %{REQUEST_URI} "/js/" [OR] 
RewriteCond %{REQUEST_URI} "/images/" [OR] 
RewriteCond %{REQUEST_URI} "/css/" 
RewriteRule (.*) $1 [L] 

#Rewrite Rules 
RewriteRule ^video/(.*) /video.php?id=$1 [L] //This gives you domain.com/video/33 
RewriteRule ^video/id/(.*) /video.php?id=$1 [L] //This gives you domain.com/video/id/33 

第一部分:^video/(.*)是你想要你的URL看起來像。 (.*)是一個變量的佔位符。第二部分/video.php?id=$1是要重寫的實際URL。 $1是將在第一部分中替換的變量。

RewriteCond規則告訴我們忽略這些文件夾,不要重寫它們。

希望這會有所幫助。 Sitepoint有一個很好的初學者文章使用mod_rewrite - http://articles.sitepoint.com/article/guide-url-rewriting

+0

我不知道這究竟是如何工作的。我嘗試在htaccess中使用以下代碼#Rewrite Rules RewriteRule^news/id /(.*)/news.php?id=$1 [L]當我嘗試使用http://mydomain.com/news打開它時/ id/33它不加載任何圖像或js或css文件,並將此代碼添加到我的所有js和css文件中。 出了什麼問題? – 2010-12-10 12:51:15

+0

已編輯主帖 – 2010-12-10 13:05:23

+0

謝謝matt。 – 2010-12-21 08:22:44