Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add regex support #19

Closed
wants to merge 2 commits into
from

Conversation

Projects
None yet
2 participants
Owner

tlovett1 commented Oct 10, 2012

Adds functionality for regex support in redirect "from" path.

@danielbachhuber danielbachhuber and 1 other commented on an outdated diff Oct 25, 2012

safe-redirect-manager.php
@@ -696,7 +711,7 @@ public function action_parse_request() {
*/
$parsed_site_url = parse_url( site_url() );
if ( '/' != $parsed_site_url['path'] ) {
- $requested_path = preg_replace( '@' . $parsed_site_url['path'] . '@i', '', $requested_path, 1 );
+ $requested_path = $requested_path = preg_replace( '@' . $parsed_site_url['path'] . '@i', '', $requested_path, 1 );
@danielbachhuber

danielbachhuber Oct 25, 2012

Contributor

Is there a particular reason this variable is declared twice?

@tlovett1

tlovett1 Oct 26, 2012

Owner

Nope. Just a typo.

@danielbachhuber danielbachhuber commented on the diff Oct 25, 2012

safe-redirect-manager.php
@@ -714,16 +730,20 @@ public function action_parse_request() {
}
// check if requested path is the same as the redirect from path
- $matched_path = ( untrailingslashit( $requested_path ) == $redirect_from );
-
- // check if the redirect_from ends in a wildcard
- if ( !$matched_path && (strrpos( $redirect_from, '*' ) == strlen( $redirect_from ) - 1) ) {
- $wildcard_base = substr( $redirect_from, 0, strlen( $redirect_from ) - 1 );
-
- // mark as match if requested path matches the base of the redirect from
- $matched_path = (substr( $requested_path, 0, strlen( $wildcard_base ) ) == $wildcard_base);
- if ( (strrpos( $redirect_to, '*' ) == strlen( $redirect_to ) - 1 ) ) {
- $redirect_to = rtrim( $redirect_to, '*' ) . ltrim( substr( $requested_path, strlen( $wildcard_base ) ), '/' );
+ if ( $enable_regex ) {
+ $matched_path = ( @preg_match( '@' . $redirect_from . '@', $requested_path ) );
@danielbachhuber

danielbachhuber Oct 25, 2012

Contributor

Best to not suppress warnings on preg_match

@tlovett1

tlovett1 Oct 26, 2012

Owner

Any reason why? If someone types in bad regex, should they see warnings? If one redirect at the top of the list has a problem and a warning is thrown, couldn't that prevent a redirect from occurring since headers have been sent?

@danielbachhuber

danielbachhuber Oct 27, 2012

Contributor

Yeah, we generally prefer the warnings to be shown rather than suppressed. We should make sure we're receiving valid regex before saving it to the database.

@danielbachhuber danielbachhuber commented on the diff Oct 25, 2012

safe-redirect-manager.php
@@ -480,6 +484,12 @@ public function action_save_post( $post_id ) {
delete_post_meta( $post_id, $this->meta_key_redirect_status_code );
}
+ if ( ! empty( $_POST['srm' . $this->meta_key_enable_redirect_from_regex] ) ) {
+ update_post_meta( $post_id, $this->meta_key_enable_redirect_from_regex, absint( $_POST['srm' . $this->meta_key_enable_redirect_from_regex] ) );
+ } else {
+ delete_post_meta( $post_id, $this->meta_key_enable_redirect_from_regex );
@danielbachhuber

danielbachhuber Oct 25, 2012

Contributor

Another thought — instead of having an option to enable regex redirects, why don't we just be smart and apply regex instead of pattern matching if the redirect_from includes # or @ or some other standard delimiter? Seems like it would be simpler.

@tlovett1

tlovett1 Oct 26, 2012

Owner

In my opinion I don't think that is simpler. Also, it is useful to be able to tell whether a redirect contains regular expressions via a meta query.

@danielbachhuber

danielbachhuber Oct 27, 2012

Contributor

Cool, works for me

Owner

tlovett1 commented Oct 27, 2012

I'm going to remove the error suppression and merge this one in manually.

@tlovett1 tlovett1 closed this in 0710cf9 Oct 27, 2012

Contributor

danielbachhuber commented Oct 27, 2012

Cool, sounds good

On Oct 26, 2012, at 22:24, Taylor Lovett notifications@github.com wrote:

I'm going to remove the error suppression and merge this one in manually.


Reply to this email directly or view it on GitHub.

@tlovett1 tlovett1 deleted the srm-regex-support branch Aug 12, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment