.htaccess重写是冲突/漂亮的网址

时间:2015-01-08 13:06:47

标签: php .htaccess url-rewriting seo pretty-urls

我有一个网址,网址如下所示: 的 example.com/sample?m=1389&t=name-title

但我希望它看起来像这样: 的 example.com/1389/name-title

我认为其中两个重写是冲突的。最上面的一个是删除每页的.php,第二个是使sample.php更加友好。这是我的整个htaccess代码。让我知道我做错了什么。我不想要文件扩展名,我希望我的页面网址看起来非常好。谢谢。

Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

# Remove www from any URLs that have them:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

Options All -Indexes

ErrorDocument 403 http://example.com/tapes.php
ErrorDocument 404 http://example.com/404

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 month"
ExpiresByType image/jpeg "access 1 month"
ExpiresByType image/gif "access 1 month"
ExpiresByType image/png "access 1 month"
ExpiresByType text/css "access 1 week"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access 1 month"
</IfModule>
## EXPIRES CACHING ##

# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# Or, compress certain file types by extension:
<files *.html>
SetOutputFilter DEFLATE
</files>

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.html$ /sample?m=$1&t=$2 [L]

1 个答案:

答案 0 :(得分:2)

您可以在根目录中使用它.htaccess:

Options +FollowSymlinks -MultiViews -Indexes
RewriteEngine on
RewriteBase /

# Remove www from any URLs that have them:
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

# remove php
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+sample\?m=([^&]+)&t=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=301,L,NE]

# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\d+)/([^/]+)/?$ sample.php?m=$1&t=$2 [L,QSA,NC]

# add php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

ErrorDocument 403 http://example.com/tapes.php
ErrorDocument 404 http://example.com/404