Search code examples
phpapacheurl-rewritingsubdirectory

SEO Friendly Link Rewrite - Unsuccessful rewrite attempts for admin subfolder - PHP Apache


My aim

if input urls are as below respectively:

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

expected rewrote urls are as below respectively:

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

.htaccess code in public_html folder

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 code in admin folder

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]

Code that I tried in .htaccess which is in admin folder

NOTES:

  1. Admin is an physical folder
  2. except /admin/... urls, every seo friendly url works at domain level.
  3. http://www.domain.p.ht/admin/index.php?adminpage=articles works
  4. http://www.domain.p.ht/admin/index.php?adminpage=comments works
  5. My .htaccess in physical admin folder All codes below were unsuccesfull, resulted in 404 not found error

    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]


    Can you please correct me? I couldn't solve my issue. I also searched stackoverflow's similar titled questions.

    BR


Solution

  • Try this htaccess code in admin folder

    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]