具有相同参数的多个页面的 htaccess 重写规则
htaccess rewrite rule for multiple pages with same paramaeters
我一直在研究以下 2 个 url 的 htaccess 重写规则:
example.com/main-page.php?pagename=web-hosting
变为 example.com/basichosting/web-hosting
example.com/sub-page.php?pagename=vps-server
变为 example.com/advancedhosting/vps-server
我使用了以下 .htaccess 重写规则并且有效
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ .php [NC,L]
RewriteRule ^basichosting/([A-Za-z0-9_'-]+)$ main-page.php?pagename= [L]
RewriteRule ^advancedhosting/([A-Za-z0-9_'-]+)$ sub-page.php?pagename= [L]
我真正想要的是我的网址看起来像
example.com/hosting/web-hosting
example.com/hosting/vps-server
是否可以将相应页面名称的'basichosting'和'advancedhosting'改为'hosting'
否则是否可以从 url 中隐藏 'basichosting' 和 'advancedhosting'。
如果可以,我该如何编写我的 htaccess 重写规则?
我的 htaccess:
RewriteEngine On
##Rules for existing php files rewrite.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/?$ .php [NC,L]
##Rules for external rewrite to hosting uri as per OP need.
RewriteCond %{THE_REQUEST} \s/main\.php\?pagename=(\S+)\s [NC]
RewriteRule ^ /hosting/%1? [R=301,L]
##Rules for internal rewrite to main.php file.
RewriteRule ^(?:[^/]*)/(.*)/?$ main.php?pagename= [L]
##Rules for external rewrite to hosting uri as per OP need.
RewriteCond %{THE_REQUEST} \s/sub\.php\?pagename=(\S+)\s [NC]
RewriteRule ^ /hosting/%1? [R=301,L]
##Rules for internal rewrite to sub.php file.
RewriteRule ^(?:[^/]*)/(.*)/?$ sub.php?pagename= [L]
<IfModule php5_module>
php_flag asp_tags Off
php_flag display_errors Off
php_value max_execution_time 30
php_value max_input_time 60
php_value max_input_vars 3000
php_value memory_limit 64M
php_value post_max_size 8M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php56"
php_value upload_max_filesize 2M
php_flag zlib.output_compression Off
</IfModule>
<IfModule lsapi_module>
php_flag asp_tags Off
php_flag display_errors Off
php_value max_execution_time 30
php_value max_input_time 60
php_value max_input_vars 3000
php_value memory_limit 64M
php_value post_max_size 8M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php56"
php_value upload_max_filesize 2M
php_flag zlib.output_compression Off
</IfModule>
<IfModule mime_module>
AddHandler application/x-httpd-ea-php56 .php .php5 .phtml
</IfModule>
使用您显示的示例,请尝试遵循 htaccess 规则。请确保在测试您的 URL 之前清除您的浏览器缓存。
RewriteEngine ON
##Rules for internal rewrite to hosting uri as per OP need.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^hosting/(.*)/?$ main.php?pagename= [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^server/(.*)/?$ sub.php?pagename= [NC,L]
##Rules for existing php files rewrite.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/?$ .php [NC,L]
<IfModule php5_module>
php_flag asp_tags Off
php_flag display_errors Off
php_value max_execution_time 30
php_value max_input_time 60
php_value max_input_vars 3000
php_value memory_limit 64M
php_value post_max_size 8M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php56"
php_value upload_max_filesize 2M
php_flag zlib.output_compression Off
</IfModule>
<IfModule lsapi_module>
php_flag asp_tags Off
php_flag display_errors Off
php_value max_execution_time 30
php_value max_input_time 60
php_value max_input_vars 3000
php_value memory_limit 64M
php_value post_max_size 8M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php56"
php_value upload_max_filesize 2M
php_flag zlib.output_compression Off
</IfModule>
<IfModule mime_module>
AddHandler application/x-httpd-ea-php56 .php .php5 .phtml
</IfModule>
JS/CS rewrite/redirect:
您可能需要使用 base 标记来修复您的 js 和其他相关资源。如果您使用相对路径链接 js 文件,那么该文件显然会得到 404,因为它正在寻找 URL 路径。例如,如果 URL 路径是 /file/ 而不是 file.html 那么您的相关资源将从 /file/ 加载,这不是目录而是重写的 html 文件。要解决此问题,请使您的链接成为绝对链接或使用 base 标记。在您网页的 header 中添加此 <base href="/">
以便您的相关链接可以从正确的位置加载。
我一直在研究以下 2 个 url 的 htaccess 重写规则:
example.com/main-page.php?pagename=web-hosting
变为 example.com/basichosting/web-hosting
example.com/sub-page.php?pagename=vps-server
变为 example.com/advancedhosting/vps-server
我使用了以下 .htaccess 重写规则并且有效
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ .php [NC,L]
RewriteRule ^basichosting/([A-Za-z0-9_'-]+)$ main-page.php?pagename= [L]
RewriteRule ^advancedhosting/([A-Za-z0-9_'-]+)$ sub-page.php?pagename= [L]
我真正想要的是我的网址看起来像
example.com/hosting/web-hosting
example.com/hosting/vps-server
是否可以将相应页面名称的'basichosting'和'advancedhosting'改为'hosting'
否则是否可以从 url 中隐藏 'basichosting' 和 'advancedhosting'。
如果可以,我该如何编写我的 htaccess 重写规则?
我的 htaccess:
RewriteEngine On
##Rules for existing php files rewrite.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/?$ .php [NC,L]
##Rules for external rewrite to hosting uri as per OP need.
RewriteCond %{THE_REQUEST} \s/main\.php\?pagename=(\S+)\s [NC]
RewriteRule ^ /hosting/%1? [R=301,L]
##Rules for internal rewrite to main.php file.
RewriteRule ^(?:[^/]*)/(.*)/?$ main.php?pagename= [L]
##Rules for external rewrite to hosting uri as per OP need.
RewriteCond %{THE_REQUEST} \s/sub\.php\?pagename=(\S+)\s [NC]
RewriteRule ^ /hosting/%1? [R=301,L]
##Rules for internal rewrite to sub.php file.
RewriteRule ^(?:[^/]*)/(.*)/?$ sub.php?pagename= [L]
<IfModule php5_module>
php_flag asp_tags Off
php_flag display_errors Off
php_value max_execution_time 30
php_value max_input_time 60
php_value max_input_vars 3000
php_value memory_limit 64M
php_value post_max_size 8M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php56"
php_value upload_max_filesize 2M
php_flag zlib.output_compression Off
</IfModule>
<IfModule lsapi_module>
php_flag asp_tags Off
php_flag display_errors Off
php_value max_execution_time 30
php_value max_input_time 60
php_value max_input_vars 3000
php_value memory_limit 64M
php_value post_max_size 8M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php56"
php_value upload_max_filesize 2M
php_flag zlib.output_compression Off
</IfModule>
<IfModule mime_module>
AddHandler application/x-httpd-ea-php56 .php .php5 .phtml
</IfModule>
使用您显示的示例,请尝试遵循 htaccess 规则。请确保在测试您的 URL 之前清除您的浏览器缓存。
RewriteEngine ON
##Rules for internal rewrite to hosting uri as per OP need.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^hosting/(.*)/?$ main.php?pagename= [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^server/(.*)/?$ sub.php?pagename= [NC,L]
##Rules for existing php files rewrite.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/?$ .php [NC,L]
<IfModule php5_module>
php_flag asp_tags Off
php_flag display_errors Off
php_value max_execution_time 30
php_value max_input_time 60
php_value max_input_vars 3000
php_value memory_limit 64M
php_value post_max_size 8M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php56"
php_value upload_max_filesize 2M
php_flag zlib.output_compression Off
</IfModule>
<IfModule lsapi_module>
php_flag asp_tags Off
php_flag display_errors Off
php_value max_execution_time 30
php_value max_input_time 60
php_value max_input_vars 3000
php_value memory_limit 64M
php_value post_max_size 8M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php56"
php_value upload_max_filesize 2M
php_flag zlib.output_compression Off
</IfModule>
<IfModule mime_module>
AddHandler application/x-httpd-ea-php56 .php .php5 .phtml
</IfModule>
JS/CS rewrite/redirect:
您可能需要使用 base 标记来修复您的 js 和其他相关资源。如果您使用相对路径链接 js 文件,那么该文件显然会得到 404,因为它正在寻找 URL 路径。例如,如果 URL 路径是 /file/ 而不是 file.html 那么您的相关资源将从 /file/ 加载,这不是目录而是重写的 html 文件。要解决此问题,请使您的链接成为绝对链接或使用 base 标记。在您网页的 header 中添加此 <base href="/">
以便您的相关链接可以从正确的位置加载。