1

我的目标

如果输入的url分别如下:

  1. somedomain.p.ht/admin/articles
  2. somedomain.p.ht/admin/comments

预期的重写网址分别如下:

  1. somedomain.p.ht/admin/index.php?adminpage=articles
  2. somedomain.p.ht/admin/index.php?adminpage=comments

public_html 文件夹中的 .htaccess 代码

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在物理管理文件夹下面的所有代码都不成功,导致 404 not found 错误

    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

4

1 回答 1

0

在 admin 文件夹中尝试这个 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]
于 2013-04-08T09:26:08.053 回答