PDA

View Full Version : .htaccess redirect after removing a part of url


Kor
10-15-2009, 11:38 AM
I have:
http://www.mydomain.com/mypage.php#somestring_anchor

I would like to redirect 301 to:
http://www.mydomain.com/mypage.php#anchor

This is not so intricate for a single page, but I have:

http://www.mydomain.com/mypage.php#somestring_anchor
http://www.mydomain2.com/somestring_mypage2.php#
http://www.otherdomain.com/mypage3.php#somestring_anchor
... and so on

In other words I would like simply to remove a certain string, nomatter where it is placed, from all the possible links (thus I need a sort of wildcard or something similar with the PHP $_SERVER['PHP_SELF'], and I don't know how to build it) within my domain and redirect to the same links, but after removing that somestring_ in all of them

Any ideas, please?

oesxyl
10-15-2009, 08:26 PM
I have:
http://www.mydomain.com/mypage.php#somestring_anchor

I would like to redirect 301 to:
http://www.mydomain.com/mypage.php#anchor

This is not so intricate for a single page, but I have:

http://www.mydomain.com/mypage.php#somestring_anchor
http://www.mydomain2.com/somestring_mypage2.php#
http://www.otherdomain.com/mypage3.php#somestring_anchor
... and so on

In other words I would like simply to remove a certain string, nomatter where it is placed, from all the possible links (thus I need a sort of wildcard or something similar with the PHP $_SERVER['PHP_SELF'], and I don't know how to build it) within my domain and redirect to the same links, but after removing that somestring_ in all of them

Any ideas, please?
try this:

RewriteRule ^(.+)sometring_(.+)$ $1$2


best regards

Kor
10-15-2009, 08:48 PM
Thank you, I will try.

As a note, I've seen that the anchors are not to be seen by the server, thus in that case (#somestring) I had to use a PHP code. Still I wonder if Apache can perform a search/replace/rewrite job, thus I will try your solution.

oesxyl
10-15-2009, 11:35 PM
Thank you, I will try.

As a note, I've seen that the anchors are not to be seen by the server, thus in that case (#somestring) I had to use a PHP code. Still I wonder if Apache can perform a search/replace/rewrite job, thus I will try your solution.
I'm sure it can because I did this in the past. I did some tests with what I post and didn't work. I remember that I used something like this in the past but after this tests I'm not sure if was only a RewriteRule or was something more complex using a RewriteCond.
I will do some tests again and I will come back with a solution, :)

best regards

oesxyl
10-16-2009, 04:07 AM
Thank you, I will try.

As a note, I've seen that the anchors are not to be seen by the server, thus in that case (#somestring) I had to use a PHP code. Still I wonder if Apache can perform a search/replace/rewrite job, thus I will try your solution.
the rewrite rule works for all cases except when somestring is in anchor.
In this last case I can't make it work. I try some combination of RewriteCond, RewriteRule and pattern using different flags but nothing work
If you want to try to solve yourself I suggest you to enable the rewrite mod log and change the log level to something grater then 0, the default level. I used 9 in my tests, :)

best regards