3

I want to rewrite my current URL that is

http://localhost/sitename/landing-page/?from=Algeria&to=Afghanistan&submit=Submit

to http://localhost/sitename/removals-from-Algeria-to-Afghanistan I have written the rewrite rule for this.

RewriteCond %{THE_REQUEST} ^GET\ /([^/]+)/landing-page/?\?(from)=([^&]+)&(to)=([^&\s]+) [NC]
RewriteRule ^ /%1/removals-%2-%3-%4-%5? [R=301,L]
RewriteRule ^removals-from-([^-]+)-to-([^-]+)$ /removalscenter/landing-page/?from=$1&to=$2&submit=Submit [L]

My htaccess code is..

 # BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /removalscenter/
RewriteRule ^index\.php$ - [L]

RewriteCond %{THE_REQUEST} ^GET\ /([^/]+)/landing-page/?\?(from)=([^&]+)&(to)=([^&\s]+) [NC]
RewriteRule ^ /%1/removals-%2-%3-%4-%5? [R=301,L]
RewriteRule ^removals-from-([^-]+)-to-([^-]+)$ /removalscenter/landing-page/?from=$1&to=$2&submit=Submit [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /removalscenter/index.php [L]
</IfModule>
# END WordPress

Now the link is changed.. but it is giving page not found error..

1 Answer 1

1

I'm assuming (looking at your RewriteRule pattern) that this .htaccess file is located in the /removalscenter directory? (Although the first couple of directives are generic, allowing for <sitename>, the remaining directives appear to be hardcoded?)

By setting a RewriteBase directive, you don't need to specify the full path for the RewriteRule substitutions - that is the reason for using RewriteBase. You can keep the RewriteRule substitution as relative (simplifying the rules) . For example:

RewriteRule . /removalscenter/index.php [L]

Can simply be:

RewriteRule . index.php [L]

http://localhost/sitename/landing-page/?from=Algeria&to=Afghanistan&submit=Submit

Your example URL does not look like the full URL. It's missing the index document eg. index.php. If you don't explicitly state this then mod_dir will need to "fix it", resulting an additional rewrite which might be causing problems. So, this should be something like:

http://localhost/sitename/landing-page/index.php?from=Algeria&to=Afghanistan&submit=Submit

Likewise, the trailing slash should not be optional in your RewriteCond directive.

In order to debug the 404 further, try temporarily changing the internal rewrite to a temporary (302) external redirect by adding an R flag. For example:

RewriteRule ^removals-from-([^-]+)-to-([^-]+)$ landing-page/index.php?from=$1&to=$2&submit=Submit [L,R]

Does it redirect? What does it redirect to?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.