.htaccess internal redirect not working with string parameter .htaccess internal redirect not working with string parameter wordpress wordpress

.htaccess internal redirect not working with string parameter


I did some try on local and found that it works for me if I put these two rules:

RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d

even before the row

RewriteRule ^store-category/(.+)/?$ /wordpress/store-category/?cid=$1 [QSA,L]

Complete .htaccess file will be:

<IfModule mod_rewrite.c>RewriteEngine OnRewriteBase /wordpress/RewriteRule ^index\.php$ - [L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^store-category/(.+)/?$ /wordpress/store-category/?cid=$1 [QSA,L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /wordpress/index.php [L]</IfModule>

Anyway, with (.+)/?$, an eventual trailing slash will be captured from the group and will go on the cid parameters. If categories doesn't have slashes inside, it's better to use ([^/]+)/?$.

Update: The understand what happen on your redirects, enable the log (see http://httpd.apache.org/docs/current/mod/mod_rewrite.html fo this). On my host I enabled mod_rewrite.c:trace3 and I get the following log in error_log (just tracking lines with rewrite)

[Fri Aug 04 12:48:56.904099 2017] [rewrite:trace3] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] strip per-dir prefix: /var/www/localhost/htdocs/wordpress/store-category/my-id -> store-category/my-id[Fri Aug 04 12:48:56.904123 2017] [rewrite:trace3] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] applying pattern '^index\\.php$' to uri 'store-category/my-id'[Fri Aug 04 12:48:56.904129 2017] [rewrite:trace3] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] strip per-dir prefix: /var/www/localhost/htdocs/wordpress/store-category/my-id -> store-category/my-id[Fri Aug 04 12:48:56.904132 2017] [rewrite:trace3] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] applying pattern '^store-category/(.+)/?$' to uri 'store-category/my-id'[Fri Aug 04 12:48:56.904143 2017] [rewrite:trace2] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] rewrite 'store-category/my-id' -> '/wordpress/store-category/?cid=my-id'[Fri Aug 04 12:48:56.904147 2017] [rewrite:trace3] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] split uri=/wordpress/store-category/?cid=my-id -> uri=/wordpress/store-category/, args=cid=my-id[Fri Aug 04 12:48:56.904151 2017] [rewrite:trace2] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] trying to replace prefix /var/www/localhost/htdocs/wordpress/ with /wordpress/[Fri Aug 04 12:48:56.904155 2017] [rewrite:trace2] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] trying to replace context docroot /var/www/localhost/htdocs with context prefix [Fri Aug 04 12:48:56.904158 2017] [rewrite:trace1] [pid 22823:tid 140368028878592] mod_rewrite.c(477): [client ::1:58312] ::1 - - [localhost/sid#788520][rid#7fa9e4002970/initial] [perdir /var/www/localhost/htdocs/wordpress/] internal redirect with /wordpress/store-category/ [INTERNAL REDIRECT]

(followed by some other logging while it try to find the default document index.php)


Regex is greedy (left to right). You're pulling in extra slashes possibly other things. I find it easiest to use not classes for URLs (rarely '.' which will match everything).

RewriteRule ^store-category/([^\/]+)/?$ store-category/?cid=$1 [QSA,L]

You may also be able to simplify to this, depending on how you want to handle urls that may be malformed after the store category id.

RewriteRule ^store-category/([^\/]+) store-category/?cid=$1 [QSA,L]

EDIT: You are right. I didn't test the whole htaccess. You're specifying the wordpress path twice one in rewritebase and again in the rule. I have updated the rules above, as well as providing a link to the updated solution in a tester:

http://htaccess.mwl.be?share=116de701-cf8a-577c-bab5-5c357d844452