Forum Moderators: phranque

Message Too Old, No Replies

Redirect with wildcards

         

Owdy

3:30 pm on Feb 17, 2010 (gmt 0)

10+ Year Member



Is it possible to redirect

[someurl.fi...]

to

[someurl.fi...]

?

* is wildcard.

If this is possible, can you show me how?

Owdy

3:37 pm on Feb 17, 2010 (gmt 0)

10+ Year Member




Arg, urls are

http://someurl.fi/index.php?topic=*

to
http://someurl.fi/somefolder/index.php?topic=*

g1smd

3:38 pm on Feb 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, it's possible.

What have you tried so far, and what did it do?

See the Forum Charter for resources to get you started. This is severer configuration code, so it is vital that you understand any code you add to your server.

Owdy

3:40 pm on Feb 17, 2010 (gmt 0)

10+ Year Member



Yeah, .htaccess naturally, but i dont have a clue how to do this.

Owdy

3:43 pm on Feb 17, 2010 (gmt 0)

10+ Year Member



This is what i have now in root, but it is there for another reason:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>


IPB forum software uses that.

g1smd

3:59 pm on Feb 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The existing code needs optimising, as well as domain canonicalisaton rules adding before the existing code.

Owdy

4:54 pm on Feb 17, 2010 (gmt 0)

10+ Year Member



Solution!


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

RewriteCond %{REQUEST_URI} /index.php
RewriteCond %{QUERY_STRING} topic=(.*)
RewriteRule .* somefolder/index.php?topic=%1 [R=301]
</IfModule>


g1smd

5:21 pm on Feb 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The existing code needs optimising, as well as domain canonicalisaton rules adding before the existing code.

The new redirect needs further optimisation and needs to go before your (not yet written) domain canonicalisation rules.

You will also need [L] on each and every rule.

[edited by: g1smd at 5:31 pm (utc) on Feb 17, 2010]

jdMorgan

5:23 pm on Feb 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Better solution:

RewriteEngine On
#
RewriteCond %{QUERY_STRING} topic=
RewriteRule ^index\.php$ http://www.example.com/somefolder/index.php [R=301,L]
#
RewriteCond $1 !^somefolder/index\.php$
RewriteCond $1 !\.(gif|jpe?g|css|js|ico)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /somefolder/index.php [L]

The <IfModule> container is not needed unless you want this code to fail silently on servers which do not have mod_rewrite installed.

The "RewriteBase /" directive is not needed, because it is simply re-declaring the default RewriteBase.

External redirects should always precede internal rewrites, to avoid exposing internally-rewritten filepaths as URLs to the clients -- possibly resulting in duplicate content and impaired search engine ranking potential.

The default behavior of RewriteRule is to pass query strings through the rule unchanged. Therefore, the testing of the query string value and the creation and use of a back-reference to this value are both unnecessary.

Rules invoking external redirects should include a protocol and a hostname, in order to avoid domain canonicalization problems.

Excluding "somefolder/index.php" and image, .css, and .js filepaths from the forum rewriterule avoids the very-expensive/inefficient file- and directory-exists checks -- These are calls to the operating system, and may result in physical disk reads -- very slow and CPU-intensive. This one change may noticeably improve your server performance on a busy site.

Changing the rewrite substitution path so that it points directly to the correct subdirectory script location avoids an external redirect on every single request for the forum script. This too may result in noticeably-better responsiveness of your site.

There are several levels of complexity: the mod_rewrite directives themselves, regular expressions, and the functional, SEO, and performance effects of the rules. Therefore, every effort should be made to fully-understand and optimize the code, and to understand exactly what the code does. It is --as g1smd pointed out-- server configuration code, and not to be installed or modified without much study and consideration.

Jim

Owdy

6:05 pm on Feb 17, 2010 (gmt 0)

10+ Year Member



Thanks jdMorgan, that work perfectly :)

Owdy

6:07 pm on Feb 17, 2010 (gmt 0)

10+ Year Member



Hmm , why is there now that image part?


RewriteCond $1 !\.(gif|jpe?g|css|js|ico)$

Owdy

6:08 pm on Feb 17, 2010 (gmt 0)

10+ Year Member



Ah, sorry, i better learn to read :)

Owdy

6:55 pm on Feb 17, 2010 (gmt 0)

10+ Year Member



I have to take it back, i use IPBoard + IP.Content as site engine and that brakes its redirects somehow.

This works with that, i just added that image exclude like you suggested


RewriteEngine On
RewriteBase /
RewriteCond $1 !\.(gif|jpe?g|css|js|ico)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

RewriteCond %{REQUEST_URI} /index.php
RewriteCond %{QUERY_STRING} topic=(.*)
RewriteRule .* foorumi/index.php?topic=%1 [R=301]

Owdy

7:00 pm on Feb 17, 2010 (gmt 0)

10+ Year Member



This works too. Maybe it needs that Base?


RewriteEngine On
#
RewriteCond %{QUERY_STRING} topic=
RewriteRule ^index\.php$ http://somesite.fi/foorumi/index.php [R=301,L]

RewriteBase /
RewriteCond $1 !^foorumi/index\.php$
RewriteCond $1 !\.(gif|jpe?g|css|js|ico)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

jdMorgan

7:46 pm on Feb 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You shouldn't need the RewriteBase at all, unless there is also another RewriteBase directive in that file that sets it to something other than "/".

If you need to use it even though there is no other RewriteBase directive in this file (which would imply a strange server configuration), then you'd might as well put it up at the top, right after the "RewriteEngine on" directive.

The modified second rule will not work as intended. So either use all of these changes or none of them:

RewriteEngine On
RewriteBase /
#
RewriteCond %{QUERY_STRING} topic=
RewriteRule ^index\.php$ http://www.example.com/foorumi/index.php [R=301,L]
#
RewriteCond $1 !^foorumi/index\.php$
RewriteCond $1 !\.(gif|jpe?g|css|js|ico)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /foorumi/index.php [L]

If this needs modification, then change only one thing at a time and test. Otherwise, it's impossible for us to tell what changes are significant, which are needed, or which may be wrong.

If you still have 'broken redirects' in your ipBoard and/or search, then post the information from your server error log, and describe the error. The code posted here is "much more correct and efficient" than what you had, and I'd hate to have you give up on the significant performance and operational improvements just because one particular directory path needs to be excluded or some other minor adjustment is needed.

As stated above, the external redirect ([R=301,L]) rule must be first, or you may suffer some very-serious, possibly-put-you-out-of-business side effects.

Jim

Owdy

8:05 pm on Feb 17, 2010 (gmt 0)

10+ Year Member



Thanks jdMorgan! I got that first one work too, just needed to change last line:


RewriteEngine On
#
RewriteCond %{QUERY_STRING} topic=
RewriteRule ^index\.php$ http://example.net/foorumi/index.php [R=301,L]
#
RewriteCond $1 !^foorumi/index\.php$
RewriteCond $1 !\.(gif|jpe?g|css|js|ico)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L]


IPB's default file looks like this:



Options -MultiViews
RewriteEngine On
RewriteBase /foorumi/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /foorumi/index.php [L]
Whats that "MultiViews" ?

jdMorgan

11:32 pm on Feb 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you change the last line, then the RewriteCond for that rule must also change from
RewriteCond $1 !^foorumi/index\.php$
to
RewriteCond $1 !^index\.php$

This excluded URL-path must match the substitution URL-path. Otherwise, this RewriteCond won't help to reduce the file-checking overhead, and it will be done twice for every request...

MultiViews is content-negotiation. If you do not need it, turn it off and leave it off. It involves a lot of overhead processing, and can seriously interfere with mod_rewrite rules.

Jim

Owdy

7:09 am on Feb 18, 2010 (gmt 0)

10+ Year Member



Yes, now it works perfectly. Thank you for your help :)