Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess internal redirect not working with string parameter

So i have been on this for hours but can't get it to resolve. Nothing on stackoverflow tackles my issue. The issue is that i have a redirect rule in my .htaccess as follows

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

This redirect works when cid is a number but when a use a string like "my-key" it does not work. With the key i have to change the rule to this.

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

However i do not want to use this because this redirects my users visibly and the url changes, which i don't want. Can anyone please explain why this thing is working for numbers but not for string parameters. Any help would be appreciated.

Edit: Complete .htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteRule ^store-category/(.+)/?$ /wordpress/store-category/?cid=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>

# END WordPress
like image 942
shazyriver Avatar asked Jul 27 '17 05:07

shazyriver


1 Answers

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{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 On
RewriteBase /wordpress/

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^store-category/(.+)/?$ /wordpress/store-category/?cid=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /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)

like image 71
Urban Avatar answered Oct 22 '22 19:10

Urban