Rectangle 27 0

php Redirect URLs with query strings in .htaccess?


# PART 2 : Redirect other old URLs
# To essentially remove the date prefix, eg. "YYYY-MM-DD-"
RewriteCond %{QUERY_STRING} ^s=/d{4}-/d{2}-/d{2}-(.+)
RewriteRule ^portfolio/post\.php$ /projecten/%1? [R=302,L]
RewriteEngine On

# PART 1 : Redirect old category URLs
RewriteCond %{QUERY_STRING} ^cat=(\w+)
RewriteRule ^portfolio/category$ /projecten/%1? [R=302,L]

# PART 2 : Redirect other old URLs
RewriteCond %{QUERY_STRING} ^s=(pagename-\w{3}-\w{3})
RewriteRule ^portfolio/post\.php$ /projecten/%1? [R=302,L]
^s=[0-9{4}]+-(.+?)/?[0-9{2}]+-(.+?)/?[0-9{2}]+-(.+?)/?(.*)$

A regex pattern such as [0-9{4}]+ isn't doing what you think it's doing. This would match any of the characters 0123456789{} 1 or more times. What you seem to be trying to do is to match exactly 4 digits. eg. [0-9]{4} (which is the same as /d{4}, using a shorthand character class).

Change the 302 (temporary) redirect to 301 (permanent) when you are sure it's working OK. 301 redirects are cached by the browser, which can make testing problematic.

I've updated my answer with respect to your revised URL structure. I've also added the ? on the substitution that I missed earlier (I see you've used the QSD flag instead - that is an alternative on Apache 2.4+).

The first part is working but the second part is not. I did not explain the right url. i will post underneath.

This assumes that the xxx in pagename-xxx-xxx are 3 literal word characters (ie. a-z, A-Z, 0-9 or _).

This is a bit of a mash, but also looks overly complex for what you are trying to achieve? For instance, why do you need to match an optional slash (ie. /?)? Your example URL does not contain any slashes?

Try the following in your root .htaccess file above any existing mod_rewrite directives.

UPDATE#1: The trailing ? on the RewriteRule substitution is necessary in order to remove the query string from the target. Or, use the QSD flag on Apache 2.4+.

UPDATE#2: With respect to the updated URL in "PART 2", try the following instead:

Yeah, i was trying to follow some examples i found to remove numbers and dashes. But obviously i did not know what i was doing... Your nwe code works perfectly, thanks a million!!!

You'll need to use mod_rewrite and a condition (RewriteCond) that matches against the QUERY_STRING server variable.

Note
Rectangle 27 0

php Redirect URLs with query strings in .htaccess?


# PART 2 : Redirect other old URLs
# To essentially remove the date prefix, eg. "YYYY-MM-DD-"
RewriteCond %{QUERY_STRING} ^s=/d{4}-/d{2}-/d{2}-(.+)
RewriteRule ^portfolio/post\.php$ /projecten/%1? [R=302,L]
RewriteEngine On

# PART 1 : Redirect old category URLs
RewriteCond %{QUERY_STRING} ^cat=(\w+)
RewriteRule ^portfolio/category$ /projecten/%1? [R=302,L]

# PART 2 : Redirect other old URLs
RewriteCond %{QUERY_STRING} ^s=(pagename-\w{3}-\w{3})
RewriteRule ^portfolio/post\.php$ /projecten/%1? [R=302,L]
^s=[0-9{4}]+-(.+?)/?[0-9{2}]+-(.+?)/?[0-9{2}]+-(.+?)/?(.*)$

A regex pattern such as [0-9{4}]+ isn't doing what you think it's doing. This would match any of the characters 0123456789{} 1 or more times. What you seem to be trying to do is to match exactly 4 digits. eg. [0-9]{4} (which is the same as /d{4}, using a shorthand character class).

Change the 302 (temporary) redirect to 301 (permanent) when you are sure it's working OK. 301 redirects are cached by the browser, which can make testing problematic.

I've updated my answer with respect to your revised URL structure. I've also added the ? on the substitution that I missed earlier (I see you've used the QSD flag instead - that is an alternative on Apache 2.4+).

The first part is working but the second part is not. I did not explain the right url. i will post underneath.

This assumes that the xxx in pagename-xxx-xxx are 3 literal word characters (ie. a-z, A-Z, 0-9 or _).

This is a bit of a mash, but also looks overly complex for what you are trying to achieve? For instance, why do you need to match an optional slash (ie. /?)? Your example URL does not contain any slashes?

Try the following in your root .htaccess file above any existing mod_rewrite directives.

UPDATE#1: The trailing ? on the RewriteRule substitution is necessary in order to remove the query string from the target. Or, use the QSD flag on Apache 2.4+.

UPDATE#2: With respect to the updated URL in "PART 2", try the following instead:

Yeah, i was trying to follow some examples i found to remove numbers and dashes. But obviously i did not know what i was doing... Your nwe code works perfectly, thanks a million!!!

You'll need to use mod_rewrite and a condition (RewriteCond) that matches against the QUERY_STRING server variable.

Note