普通网友 2015-01-08 13:06
浏览 46
已采纳

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

I have a website where the url looks like this: example.com/sample?m=1389&t=name-title

but I want it to look like this: example.com/1389/name-title

I think two of the rewrites are conflicting. The top one is to remove the .php off of every page and the second one is to make the sample.php more seo friendly. Here's my whole htaccess code. Let me know what I'm doing wrong. I don't want file extensions and I want my page urls to look really nice. Thank you.

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条回答 默认 最新

  • douluan5523 2015-01-08 15:21
    关注

    You can use that in your root .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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?