如何使用 htaccess 使我的应用程序 url 漂亮(SEF)

发布于 2024-10-07 04:02:15 字数 5028 浏览 2 评论 0原文

这是我的应用程序使用的 url,

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

我想让 m 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 和正则表达式,我将不胜感激。

另外,我想禁用文件查看选项索引或我不记得确切的东西。

谢谢..

this is the url my application uses

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

i want to make m url look clean with htaccess like

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

the problem is older application already uses lot of htaccess codes in the root directory. here is the code which already exist in 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>

what code i should be using for Rewrite_rule and also i would appreciate if someone could guide me to the tutorial where i would be able to understand htaccess and also the regular expressions.

additionally i would want to disable the file viewing Option Indexes or something which i don't remember exactly.

thank you..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰葑 2024-10-14 04:02:15

尝试以下操作

#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

Try the following

#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

The first section: ^video/(.*) is what you want your URL to look like. The (.*) is a placeholder for a variable. The second section /video.php?id=$1 is the actual URL to be rewritten. The $1 is the variable that will be replaced in the first section.

The RewriteCond rules tell us to ignore these folders and not rewrite them.

Hope this helps. Sitepoint have a good beginners article on using mod_rewrite - http://articles.sitepoint.com/article/guide-url-rewriting

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文