Home > Blockchain >  SEO friendly url is not working in server but it works in cpanel
SEO friendly url is not working in server but it works in cpanel

Time:11-03

I am trying to do an SEO-friendly URL in the CloudWays server but it's not working. Also, When I try it in the localhost or Cpanel it works fine.

Thanks!

This is my .htaccess file code:-

Options  MultiViews
RewriteEngine On

# Set the default handler
DirectoryIndex index.php index.html index.htm

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php/$1 [L]

RewriteRule ^validate/([a-zA-Z0-9-/] )$ search.php?phoneNumber=$1
RewriteRule ^validate/([a-zA-Z-0-9-] )/ search.php?phoneNumber=$1

This is the main link:-

https://example.com/search.php?phoneNumber=16503858068

And I want it like this:-

https://example.com/validate/16503858068

CodePudding user response:

Look at RewriteRule (.*) /index.php/$1 [L].

The L option means the parser stops at this redirect rule if it matches. Since (.*) will match everything the rule is applied and the following rules will not be applied ever.

To fix this you can prepend your own rules to this rule, but make sure to use the Lflag as well else the (.*) rule will overwrite it again.

That's also answered here: multiple rewriterules

CodePudding user response:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php/$1 [L]

RewriteRule ^validate/([a-zA-Z0-9-/] )$ search.php?phoneNumber=$1
RewriteRule ^validate/([a-zA-Z-0-9-] )/ search.php?phoneNumber=$1

Your rules are in the wrong order. The last two rules (which can be combined into one) are never processed because a URL of the form /validate/16503858068 is routed to /index.php by the preceding rule.

Try it like this instead:

# Disable MutliViews
Options -MultiViews

RewriteEngine On

# Set the default handler
DirectoryIndex index.php index.html index.htm

RewriteRule ^validate/([a-zA-Z0-9-] )/?$ search.php?phoneNumber=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php/$1 [L]

Note the L flag on the first rule.

Also, you should probably disable MultiViews. For some reason you were explicitly enabling this?

  • Related