我一直在为以下两个urls编写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 ^(.*)$ $1.php [NC,L]
RewriteRule ^basichosting/([A-Za-z0-9_'-]+)$ main-page.php?pagename=$1 [L]
RewriteRule ^advancedhosting/([A-Za-z0-9_'-]+)$ sub-page.php?pagename=$1 [L]我真正想要的是我的urls看起来像
example.com/hosting/web-hosting
example.com/hosting/vps-server
是否可以将相应页名的“基本托管”和“高级托管”更改为“宿主”?
否则,是否有可能隐藏‘基本托管’和‘高级托管’对urls。
如果是这样,我如何编写htaccess重写规则?
我的htaccess:
RewriteEngine On
##Rules for existing php files rewrite.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/?$ $1.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=$1 [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=$1 [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>发布于 2021-07-07 05:41:53
在显示的示例中,请尝试遵循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=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^server/(.*)/?$ sub.php?pagename=$1 [NC,L]
##Rules for existing php files rewrite.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/?$ $1.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重写/重定向:您可能需要使用基标记来修复js和其他相关资源。如果您使用相对路径链接js文件,那么该文件显然会得到404,因为它正在查找URL路径。例如,如果URL路径是/file/而不是file.html,那么您的相对资源将从/file/加载,它不是一个目录,而是重写的html文件。若要解决此问题,请将链接设置为绝对链接或使用基本标记。在网页的标题中添加这个<base href="/">,这样您的相对链接就可以从正确的位置加载。
https://stackoverflow.com/questions/68280572
复制相似问题