SEO友情链接重写 - 管理子文件夹的不成功重写尝试 - PHP Apache

时间:2013-04-08 09:01:28

标签: php apache url-rewriting subdirectory

我的目标

如果输入网址分别如下所示:

  1. somedomain.p.ht/admin/articles
  2. somedomain.p.ht/admin/comments
  3. 预期的重写网址分别如下:

    1. somedomain.p.ht/admin/index.php?adminpage=articles
    2. somedomain.p.ht/admin/index.php?adminpage=comments
    3. .htaccess代码在public_html文件夹

      RewriteEngine On
      RewriteBase /
      
      #always use www - redirect non-www to www permanently
      RewriteCond %{HTTP_HOST} !^www\.
      RewriteCond %{HTTPS}s on(s)|
      RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
      
      
      # hotlink protection
      RewriteCond %{HTTP_REFERER} !^$
      RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?domain.p.ht [NC]
      RewriteRule \.(jpg|jpeg|png|gif|css|js)$ - [NC,F,L]
      
      # 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
      
      # File caching is another famous approach in optimizing website loading time
      <FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf)$">
      Header set Cache-Control "max-age=2592000"
      </FilesMatch>
      
      # disable directory browsing
      Options All -Indexes
      
      # secure htaccess file
      <Files .htaccess>
       order allow,deny
       deny from all
      </Files>
      
      # secure password file
      <Files .htpasswd>
       order allow,deny
       deny from all
      </Files>
      
      # secure spesific files
      <Files a_secret_file.php>
       order allow,deny
       deny from all
      </Files>
      
      #SEO friendly linking
      RewriteEngine On
      RewriteBase /
      RewriteRule ^articles/(.+)/(.+)$ index.php?page=articles&subject=$1&object=$2 [L]
      RewriteRule ^articles/(.+)$ index.php?page=articles&subject=$1 [L]
      RewriteRule ^labels/(.+)$ index.php?page=labels&subject=$1 [L]
      RewriteRule ^(contact|labels|articles)$ index.php?page=$1 [L]
      
      管理员文件夹中的

      .htaccess代码

      AuthUserFile /path/to/public_html/.htpasswd
      AuthName "Log In"
      AuthType Basic
      Require valid-user
      
      RewriteEngine On
      RewriteBase /
      # SEO friendly linking in physical admin folder
      RewriteRule ^admin/(articles|comments|questions)$ admin/index.php?adminpage=$1 [L]
      

      我在管理文件夹

      中的.htaccess中尝试过的代码

      注意:

      1. Admin是一个物理文件夹
      2. 除了/ admin / ... url,每个seo友好的url都在域级别工作。
      3. http://www.domain.p.ht/admin/index.php?adminpage=articles正常工作
      4. http://www.domain.p.ht/admin/index.php?adminpage=comments正常工作
      5. 我在物理管理员文件夹中的.htaccess 以下所有代码均为unsuccesfull,导致404未找到错误

        RewriteRule ^admin/comments$ /admin/index.php?adminpage=$1

        RewriteRule ^admin/(articles|comments|questions)$ admin/index.php?adminpage=$1 [L]

        RewriteRule ^/admin/(articles|comments|questions)$ /admin/index.php?adminpage=$1 [L]


        你能纠正我吗?我无法解决我的问题。我还搜索了stackoverflow的类似标题问题。

        BR

1 个答案:

答案 0 :(得分:0)

在管理文件夹

中尝试此htaccess代码
AuthUserFile /path/to/public_html/.htpasswd
AuthName "Log In"
AuthType Basic
Require valid-user

RewriteEngine On
RewriteBase /admin/

# SEO friendly linking in physical admin folder
RewriteRule ^(articles|comments|questions)/?$ index.php?adminpage=$1 [L,QSA,NC]