douqiaosu0280 2014-10-19 19:27
浏览 24
已采纳

使用htaccess制作seo友好链接时403错误

Thanks to your help guys I was able to make an .htaccess that rewrites the dynamic links into static one. Now I would like to to extend that solution onto the whole site so that I load accurate module based on $_get variable. Then thanks to .htaccess I should be able to make seo friendly links. The only issue is... it's not working... It shows me an error:

403 Forbidden You don't have permission to access this document.

I am using this script to rewrite the links:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /mod1/

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/mod1/index.php\?module=([^\s&]+) [NC]
RewriteRule ^ /mod1/%1? [R=302,L]

# internal forward from pretty URL to actual one
RewriteRule ^mod1/([^\s&]+)?$ mod1/index.php?module=$1 [L,QSA]

Right now I am using the folder mod1 but normally I would like to have there a domain, so that it would have:

www.domain.com/index.php?module=create_list

to be redirected to:

www.domain.com/create_list

I have found an information on the net that I should change the permissions of the file I target (which is index in my case) to 755 but it didn't work in my case. Another mystery of that script is that the first part seems to work, but I am not sure why it always gives the url "/" at the end, even if I did not defined it in the rule.

Any help appreciated.

EDIT:

I manage to fix that problem with mall changes to the script:

Options +FollowSymlinks -MultiViews
RewriteEngine On

RewriteBase /mod1/

#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/mod1/index.php\?module=([^\s&]+) [NC]
RewriteRule ^ %1.html? [R=302,L]

# internal forward from pretty URL to actual one
RewriteRule ^([^/]+).html?$ index.php?module=$1 [L,QSA]

It is working poperly. However I still do not understand few things. Firstly if I remove the # from:

#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d

The [L,QSA] still works but the permament [R=302,L] not, I understand that those two line are for files that are used with the php file and they fix the problem with pathways. But how to make them work together w permament redirect 302?

Second thing is I needed to add .html at the end of static link, which was not part of my plan. Is it possible to make it work without .html extension?

  • 写回答

1条回答 默认 最新

  • duanqi5333 2014-10-19 19:39
    关注

    If you want to have more than one variable passed from SEO url back to your index.php you could do something like:

    Options +FollowSymlinks -MultiViews
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^([^/]+)$ index.php?module=$1
    RewriteRule ^([^/]+)/([^/]+)$ index.php?module=$1&var2=$2
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?module=$1&var2=$2&var3=$3
    

    Make sure to put that .htaccess file in your mod1/ directory and remove the old one.

    If you access /mod1/search the request will end in index.php?module=search.

    /mod1/search/Books => index.php?module=search&var2=Books

    mod1/search/Books/20 => index.php?module=search&var2=Books&var3=20

    I don't see any point why you would redirect /mod1/index.php?module=something to /mod1/something because this will lead to endless loop:

    1. User sends /mod1/index.php?module=something => Server redirects it to /mod1/something
    2. User sends /mod1/something => Server redirects it to /mod1/index.php?module=something

    and so on... Not to mention the duplicated content you'll end with from the search engine's point of view.

    EDIT 1: If you want to make the trailing slash optional, just append /? at the end of the RewriteRule:

    RewriteRule ^([^/]+)/?$ index.php?module=$1
    RewriteRule ^([^/]+)/([^/]+)/?$ index.php?module=$1&var2=$2
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?module=$1&var2=$2&var3=$3
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line