简体   繁体   中英

htaccess rewrite rule for multiple pages with same paramaeters

I have been working on htaccess rewrite rule for below 2 urls:

example.com/main-page.php?pagename=web-hosting which changes to example.com/basichosting/web-hosting

example.com/sub-page.php?pagename=vps-server which changes to example.com/advancedhosting/vps-server

I use the following .htaccess rewrite rule and it works

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]

What I actually want is my urls to look like

example.com/hosting/web-hosting

example.com/hosting/vps-server

Is it possible to change 'basichosting' and 'advancedhosting' to 'hosting' for the corresponding page name

Or else is it possible to hide 'basichosting' and 'advancedhosting' from the urls.

If so how can I write my htaccess rewrite rule?

My 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>

With your shown samples, please try following htaccess Rules. Please make sure to clear your browser cache before testing your URLs.

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 rewrite/redirect: You may need to use base tag to fix your js and other relative resources. If you are linking js files using a relative path then the file will obviously get a 404 because its looking for URL path. for example if the URL path is /file/ instead of file.html then your relative resources are loading from /file/ which is not a directory but rewritten html file. To fix this make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM