Friday, August 4, 2017

.htaccess internal redirect not working with string parameter

Leave a Comment

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 

4 Answers

Answers 1

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)

Answers 2

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

Answers 3

Use this, this is successfully working for me i hope it will works for you.

RewriteEngine on IndexIgnore *  RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond $1 !^(index\.php|img|robots\.txt) RewriteRule ^(.*)$ index.php/$1 [L]  RewriteRule ^wordpress/index.php/.*$ - [F,L,NC] 

Answers 4

Have yo tried this? This was worked for me in my WP site.

RewriteCond %{REQUEST_URI}  ^/page\.php$ RewriteCond %{QUERY_STRING} ^id=(.+)/?$ RewriteRule ^(.*)$ http://example.site/page/%1.pdf [R=302,L] 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment